problem with data validation preg_match

Asked

Viewed 172 times

1

hello, good night to everyone I am with a problem in the question of form validation where my preg_match works perfectly however not allow spaces anyone knows tell me how to let the user insert spaces between words?if(preg_match("/^[a-zA-Z0-9]+?$/i", $string)){

2 answers

1

Try to do it this way:

$string = 'palavra com espaço 123';
if (preg_match("/[\sa-zA-Z0-9]+?$/i", $string)) {
    echo '<pre>'; print_r($string); echo '</pre>';
}

Adding a \s which matches any blank character.

  • thank you worked out too

1


Just add a s to your regex, like this:

[a-za-Z d s]

I also traded 0-9 for d just to show that you can do it like this.

  • Thanks buddy worked out

  • and if I also want to release the character ' @ and ' _ ' ?

  • Truth had forgotten the \d :P, value @Adrianoback

Browser other questions tagged

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