70

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.

8 Answers 8

110

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]+"

Sign up to request clarification or add additional context in comments.

6 Comments

Just what i was looking for, thanks. Could you explain what the differce is between any kind of space with \s and just the ` ` space.
\s is a class containing tab and space characters.
if i type only spaces,then it accepts.whats should be my pattern which accepts spaces but not if ONLY space is entered?
This will not work with diacritics, i.e.: ą, ó, ä, ö
@bumerang how could we easily include those?
|
47

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

7 Comments

that's really neat, never saw that before. -- managed to found it, now that I know what it's called: stackoverflow.com/a/30225759/2848941 Very useful, thanks!
What I had been using before (and it seemed to work, but I don't understand it, was this: [a-zA-Z\u00c0-\u00d6\u00d8-\u00f6\u00f8-\u024f ]+
You can simply use max-length and min-length attributes instead of using the {2,} bit. Regardless I like this answer; especially for the clarifications you took the time to make, +1.
please don't forget apostrophes. Us Irish take the brunt of this anti apostrophe discrimination!
@eddyoc Can you then please provide in comment, or in my answer edit necessary characters range?
|
11

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:

pattern="([^\s][A-z0-9À-ž\s]+)"

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 ą, ó, ä, ö.

Comments

10

To avoid an input with only spaces, use: "[a-zA-Z0-9]+[a-zA-Z0-9 ]+".

eg: abc | abc aBc | abc 123 AbC 938234

To ensure, for example, that a first AND last name are entered, use a slight variation like

"[a-zA-Z]+[ ][a-zA-Z]+"

eg: abc def

2 Comments

Or I believe you can just include the 'required' attribute to circumvent that.
@user1893702 The required attribute will accept spaces. You're Gonna Need a Bigger Regex.
4

Use this code to ensure the user doesn't just enter spaces but a valid name:

pattern="[a-zA-Z][a-zA-Z0-9\s]*"

Comments

3

enter image description here

<h1>In my case, I need only Number and I hafta stop to user entering any Alphabets. We can also stop to entering any number.</h1>

<hr> 
<p>
<h2>Number only</h2>
<input type="tel" name="PhoneNumber" onkeyup="this.value=this.value.replace(/[^0-9]/g,'');" />
</p>
<hr> 
<p>
<h2>Alphabets only</h2>
<input type="text" name="name" onkeyup="this.value=this.value.replace(/[^A-z]/g,'');" />
</p>

1 Comment

This solution is 'smart'. However not all users are. I'd recommend to have an error message to explain exactly why the input isn't valid rather than change the input of the user.
0

Use Like below format code

$('#title').keypress(function(event){
    //get envent value       
    var inputValue = event.which;
    // check whitespaces only.
    if(inputValue == 32){
        return true;    
    }
     // check number only.
    if(inputValue == 48 || inputValue == 49 || inputValue == 50 || inputValue == 51 || inputValue == 52 || inputValue == 53 ||  inputValue ==  54 ||  inputValue == 55 || inputValue == 56 || inputValue == 57){
        return true;
    }
    // check special char.
    if(!(inputValue >= 65 && inputValue <= 120) && (inputValue != 32 && inputValue != 0)) { 
        event.preventDefault(); 
    }
})

1 Comment

Characters 91 to 96 are special characters. Should be event.preventDefault(); for that range
-1

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"

1 Comment

The question seems to be looking for a validation pattern. Also, please note that keyboard event properties like charCode, keyCode, and which will be deprecated and replaced by key: developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.