Skip to content
Advertisement

preg_match() and username

function isUserID($username) {
  if (preg_match('/^[a-zd_]{2,20}$/i', $username)) {
    return true;
  } else {
    return false;
  }
}   

Easy one.., i have this, can you explain what it checks for? I know it checks if the username have length between 2-20, what more? Thanks

Advertisement

Answer

It searches for text containing only alphanumeric and underscore characters, from 2 to 20 characters long.

/^[a-zd_]{2,20}$/i
||||  | |||     |||
||||  | |||     ||i : case insensitive
||||  | |||     |/ : end of regex
||||  | |||     $ : end of text
||||  | ||{2,20} : repeated 2 to 20 times
||||  | |] : end character group
||||  | _ : underscore
||||  d : any digit
|||a-z: 'a' through 'z'
||[ : start character group
|^ : beginning of text
/ : regex start
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement