How can I allow special characters for a username?

Viewed 316

Is there any way to allow characters such as "-" and "." (underscore and full-stop) in a username? Right now I'm using the ctype_alnum() function, but I don't know how to let the user use "." and "-".

if (ctype_alnum($uid) == false) {
      $uidERR = "Only letters and numbers are allowed";
    }
1 Answers

You could use a regular expression:

if (!preg_match('/^[a-zA-Z0-9._-]+$/', $uid)) {
    $uidERR = "Only letters and numbers are allowed";
}

This will match all uppercase and lowercase letters, numbers, ., _, and -.

Related