Using preg_match

2

With this code I can block special characters in my input , however I want it to still 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.'
    ));
} 
    
asked by anonymous 06.12.2017 / 17:24

1 answer

3

You can use this expression:

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

It will only return true if $username has:

\w Alpha-numeric character. Letters (uppercase and lowercase), numbers and _ (underline)

- Hyphen

. Point

@ Arroba

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

See Ideone .

    
06.12.2017 / 18:11