-2

I have a form with a Name field. I want to allow a user to be able to add a space, two words (first name and second name) or a hyphen but nothing else.

i.e names like Jo-Anne, Harry James, L S should be accepted but nothing like Harry James88, John@, KinG R3ginald etc.

This is my code in the source:

<input name="firstname" pattern="^[A-Za-z]+$" required="required" type="text" />

Please help me with the correct regex to be using here.

Thanks!

Jason475
  • 25
  • 5
  • Try `pattern="[A-Za-z]+[-\s][A-Za-z]+"` – Wiktor Stribiżew Dec 02 '20 at 11:40
  • Hi, thanks for your input. Unfortunately this does not work, I want a user to be able to write their name either with or without spaces, hyphens etc. The regex you provided is making it that the field MUST contain a hyphen or space, whereas I need it to allow it but not force it – Jason475 Dec 03 '20 at 06:11

1 Answers1

-2

Answering my own question - found a solution via Googling...

^[a-zA-Z-( )]+$

Jason475
  • 25
  • 5