Jump to content

validating alphanumeric password


thecommonthread

Recommended Posts

ctype_alnum by itself only checks that it is an alphanumeric string.  It does not determine whether it has at least one number in it.

 

if (preg_match('~^[a-z0-9]*[0-9][a-z0-9]*$~i',$subject)) {
  // $subject is alphanumeric and contains at least 1 number
} else {
  // failed
}

 

OP: This code will do what you want with one caveat: Technically a string consisting of a single digit like "9" will pass.  If you want to do other things like enforce a minimum/maximum length, be more specific.

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.