简体   繁体   中英

Regex for user login verification

I am trying to use regex to check if a user name is considered valid. For a user name to be valid it must be between 2 and 10 characters long. It must start with a lower case letter and it must be made up of only lowercase letters, upper case letters or digits.

Here is what I have so far

return preg_match("/^[a-z]([a-zA-Z0-9]{2,10})$/", $name);

For consistency, your capture expression should include your whole username (in your case you are skipping the first letter). You've also noticed you only need to use {1,9} for the second half:

^([a-z]{1}[a-zA-Z\d]{1,9})$
  • this is matching one lowercase letter to begin with, followed by between 1 and 9 a-zA-Z0-9 s afterwards, and capturing the whole username.

Modifiers (if required): mg (multi-line, match-all [for when testing with a block of multiple lines])

Demo: http://regex101.com/r/qW6kN5

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