Ann
Ann

Reputation: 377

HTML input pattern only 1 space

I want a field which accept just letters and maximum one space. It is for a name. The name can be in two formats: "James" or "John James" (firstName or firstName lastName). Not case sensitive. The pattern i have right now allows an infinite of letters and spaces, but not white space as first character.

<input type="text" name="Name id="name" required pattern="[a-zA-Z][a-zA-Z\s]*" title="This field is required">

The pattern is taken from internet. I don't know why it has 2 brackets, why is a star at the end(guessing means 'infinite') and also why is the space character in the second bracket

Upvotes: 2

Views: 4478

Answers (1)

Omari Victor Omosa
Omari Victor Omosa

Reputation: 2869

Try this

1st one This 1st one you can only enter up to a maximum of two words because it will only allow only a single space

pattern="^[a-zA-Z]+\s?[a-zA-Z]+$"

2nd one This second one can allow as many words as you can but will only allow only a single space between each words

pattern="^[a-zA-Z]+( [a-zA-Z]+)*$"

Upvotes: 2

Related Questions