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)){
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)){
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.
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.