-1

i'm trying to not allow the users to introduce special caracters inside the name input box , but it also doesn't allow the user to leave an empty space between his first and last name.

I'm using a built it php function for the validation.

Does anyone know what should i add to it to allow empty spaces?

Here is the line.

 elseif (!preg_match("/^[a-zA-Z0-9]*$/", $name)) {
    $errorName = true;
}
  • 1
    Just add a space to your character class. If you want to enforce a single space, just repeat your character class with a space between them. Note that these restrictions are rarely a good idea. "John O'Connel" and "Luis Bruñel" would both fail your updated validation. – miken32 May 25 '21 at 18:16

1 Answers1

1

You could do ^[a-zA-Z0-9]*( [a-zA-Z0-9]*)?$

https://regex101.com/r/KIY77S/2

to allow a single space, or simply ^[a-zA-Z0-9 ]*$ to allow any number of spaces

dave
  • 62,300
  • 5
  • 72
  • 93