Use of the preg_match

Asked

Viewed 297 times

2

With this code I can block special characters in my input, but I still want him to accept ., - and _ how to proceed?

elseif (!preg_match('/^[a-z A-Z0-9]+$/', $username)) {
    echo json_encode(array(
        'login'     => false,
        'message' => 'Existem caracteres especiais no seu nome de usuário, se estiver utilizando <strong>@</strong>, remova-o.'
    ));
} 
  • Add them to the list (what content is between the brackets).

  • @rray [a-z A-Z0-9 . - _] thus ?

  • 1

    Spaceless: [a-zA-Z0-9_.-]

  • Just a question, this will also block if user tries to log in with email ?

  • 1

    It will only allow the characters that are on the list, if you put a @ will 'block' and execute the echo and json_encode()

1 answer

1


You can use this expression:

/^[\w-.@]+$/

Will only return true if the $username has:

\w Alpha-numeric character. Letters (upper and lower case), numbers and _ (underline)

- Hyphen

. Dot

@ Arroba

Any character other than those listed above, the preg_match will be false.

See on Ideone.

  • And it really worked, thanks @Dvd

Browser other questions tagged

You are not signed in. Login or sign up in order to post.