Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML5 form validation pattern alphanumeric with spaces?

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.

like image 876
RTB Avatar asked Oct 27 '13 14:10

RTB


People also ask

How do I add special characters to a pattern in HTML?

After a bit of trial and error, turns out you can use pattern="^[\p{L}]${1,25} to allow ASCII letters and Unicode variants, accented letters and special characters.

How do I validate only alphabets in HTML?

You can write a JavaScript form validation script to check whether the required field(s) in the HTML form contains only letters. To get a string contains only letters (both uppercase or lowercase) we use a regular expression (/^[A-Za-z]+$/) which allows only letters.

How do you validate a name field in HTML?

The validation process evaluates whether the input value is in the correct format before submitting it. For example, if we have an input field for an email address, the value must certainly contain a valid email address; it should start with a letter or a number, followed by the @ symbol, then end with a domain name.

How do I validate a pattern in HTML?

Specifying a pattern You can use the pattern attribute to specify a regular expression that the inputted value must match in order to be considered valid (see Validating against a regular expression for a simple crash course on using regular expressions to validate inputs).


2 Answers

How about adding a space in the pattern attribute like pattern="[a-zA-Z0-9 ]+". If you want to support any kind of space try pattern="[a-zA-Z0-9\s]+"

like image 116
Lachezar Avatar answered Sep 21 '22 13:09

Lachezar


My solution is to cover all the range of diacritics:

([A-z0-9À-ž\s]){2,} 

A-z - this is for all latin characters

0-9 - this is for all digits

À-ž - this is for all diacritics

\s - this is for spaces

{2,} - string needs to be at least 2 characters long

like image 23
bumerang Avatar answered Sep 24 '22 13:09

bumerang