Jump to content

[SOLVED] How do constrain a form input string to alphanumeric characters?


sardonicgem

Recommended Posts

Hello folks,

 

I'm trying to check a form field to make sure the user inputs an alphanumeric entry only.

 

The form field is case sensitive. Also I'm not quite sure how preg_match works.

 

php.net indicates:

 

preg_match() returns the number of times pattern  matches. That will be either 0 times (no match) or 1 time because preg_match() will stop searching after the first match.

 

So suppose my string was "Pa55word"

 

preg_match( '[:alnum:]',"Pa55word")

 

(1)So according to the return value conditions, will preg_match stop searching after the first letter P?

 

(2)Does anyone know of a function that would service these types of conditions? If not, I suppose I could write one to store all the characters of the string in an array and then run a match comparison on each element.  Although this does not seem optimal at all. Any suggestions?

 

I happen to like ereg() and eregi() more.

 

if (eregi('[a-zA-Z0-9]+', $var) {

  echo "Good!";

}

else {

  echo "Bad...";

}

 

Obviously, put in your variable and your own stuff in the if {} else {} blocks.

 

I think I read ereg will be taken out of PHP 6 core and only available as a compiled option.  I'm using preg for all future projects,

never know when my code might end up on a PHP 6 server with no ereg available ;)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.