lundi 3 décembre 2018

Username must begin with a letter - PHP

My objective is to let the form warn a user if the username input does not start with a letter. Unfortunately, the condition is still true despite the entered username starting with a letter.

The code below is the variable which contains an array for storing all 26 both uppercase and lowercase letters.

$alphas = array_merge(range('a', 'z'), range('A', 'Z'));

Then the code below is used to output the error message.

if (empty($username)){
    array_push($errors, "*Username cannot be blank<br>");
}else if(!preg_match("/^[a-zA-Z0-9_]*$/",$username)){
    array_push($errors, "*Username can only contain alpha-numeric characters or an underscore<br>");
}else if($username[0] !== $alphas){
    array_push($errors, "*Username must begin with a letter<br>");
}

The condition is "if($username[0] !== $alphas)", however it keeps returning TRUE, meaning it will show the message "*Username must begin with a letter" regardless the first letter of the username input.

How can I get through this problem?

Aucun commentaire:

Enregistrer un commentaire