I have the following input tag in my html5 form:
<p>
<label>Company Name*</label>
<input type="text" name="name" class="field" required pattern="[a-zA-Z0-9]+" />
</p>
This works just fine checking if the company name consists out of alphanumeric characters. But of course I want to allow spaces in the company name. I need to know what I should add to the pattern.
It's quite an old question, but in case it could be useful for anyone, starting from a combination of good responses found here, I've ended using this pattern:
It will require at least two characters, making sure it does not start with an empty space but allowing spaces between words, and also allowing special characters such as
ą, ó, ä, ö
.Use Like below format code
Use this code to ensure the user doesn't just enter spaces but a valid name:
Use below code for HTML5 validation pattern alphanumeric without / with space :-
for HTML5 validation pattern alphanumeric without space :- onkeypress="return event.charCode >= 48 && event.charCode <= 57 || event.charCode >= 97 && event.charCode <= 122 || event.charCode >= 65 && event.charCode <= 90"
for HTML5 validation pattern alphanumeric with space :-
onkeypress="return event.charCode >= 48 && event.charCode <= 57 || event.charCode >= 97 && event.charCode <= 122 || event.charCode >= 65 && event.charCode <= 90 || event.charCode == 32"
How about adding a space in the pattern attribute like
pattern="[a-zA-Z0-9 ]+"
. If you want to support any kind of space trypattern="[a-zA-Z0-9\s]+"
To avoid an input with only spaces, use:
"[a-zA-Z0-9]+[a-zA-Z0-9 ]+"
.To ensure, for example, that a first AND last name are entered, use a slight variation like
"[a-zA-Z]+[ ][a-zA-Z]+"