problem with data validation preg_match

1

Hello everyone, I have a form validation problem where my preg_match works perfectly, but it does not allow spaces, does anyone know how to let the user insert spaces between words? if(preg_match("/^[a-zA-Z0-9]+?$/i", $string)){

    
asked by anonymous 19.09.2017 / 02:04

2 answers

1

Just add a \ s to your regex, like this:

  

[a-zA-Z \ d \ s]

I also changed the 0-9 by \ d just to show what you can do.

    
19.09.2017 / 02:24
1

Try to do this:

$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 that matches any whitespace character.

    
19.09.2017 / 02:22