简体   繁体   中英

Regex only allow alphanumeric and at least two characters

I am using PHP and have a string that I need to validate that must be...

  • Only AZ and 0-9
  • Not one character in its own
  • Can not be just numbers on their own
  • Can be just letters on their own

I have what I think is the correct regex for only Az and 0-9 which is...

if(preg_match('/[^a-z_\-0-9]/i', $string))
{
  echo "not valid string";
}

but how do I add to this to also ensure that it is at least two characters long?

I would look at

^[a-zA-Z0-9]{2,}$

start string, any character in a-z or A-Z or 0-9, matched two or more times, end string.

http://regex101.com/r/sN7tX4

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM