Validating registration with filter validate regexp

Asked

Viewed 79 times

0

I’m using filter validate regexp this way:

filter_input(INPUT_POST, 'nome', FILTER_VALIDATE_REGEXP, array("options" => array("regexp" => "/.[a-zA-ZÀ-ú\s]+$/"))

The validation works, the strange thing is that if I put only one letter in the form field, it slashes. For example: If I put only "a" it bar, but if I put "aa" it works. Only from two letters it validates.

1 answer

1


Is because of the . in the beginning, which is considered the search for "a character" and more [a-zA-ZÀ-ú\s]+, that would be the second or more characters, see the difference:

also do not forget the ^ to require the string to begin and end (with $) exactly as the regex defines, because if not it will require only that you finish, but the beginning may have any input format

Enjoy and also add the modify i, for case-insensitive, so you won’t need A-Z and a-z, stay like this: /^[a-zà-ú\s]+$/i

Then change to:

filter_input(INPUT_POST, 'nome', FILTER_VALIDATE_REGEXP, array("options" => array("regexp" => "/^[a-zà-ú\s]+$/i"))

Note that for unicode, if you are using UTF-8 for example, you may need to use the modifier u, thus: /^[a-zà-ú\s]+$/iu

  • I put this one: / [a-zà-ù s]+$/i and this one: / [a-zà-ù s]+$/Iu. It keeps giving the same problem.

  • I had testing before without the stitch too, but was giving this problem.

  • Oops! Got it here! I forgot to update. rsrs

Browser other questions tagged

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