lena
lena

Reputation: 143

How to check if a string containing space is alphabet or not in jquery?

I have written a code in jquery to check if a word is alphabet or not. But I want to write the code for checking a string which contains space also.

    var letters = /^[A-Za-z]+$/;
    if(!(name.match(letters))){

    var msg = "Enter a valid name !!!";
    document.getElementById("resp").innerHTML = msg;
    event.preventDefault();   

    }

for the input field :

  <input type="text" name="name" id="namee" placeholder="Full Name" pattern="[a-zA-Z]+" required>

When i am inputing a name having two words, the message valid name is showing becos the space character is read as a invalid character. Can anyone suggest how to correct this ?

Upvotes: 0

Views: 76

Answers (2)

Sinned Lolwut
Sinned Lolwut

Reputation: 182

You may use the regex

/^[A-Za-z ]+$/ 

or

/^[A-Za-z\s]+$/

Upvotes: 1

Karol Gasienica
Karol Gasienica

Reputation: 2904

Your regex could look like following:

[A-Za-z\s]+

So it will be:

var letters = /^[A-Za-z\s]+$/;

In this case \s means space.

Manual

More about \s you can read here

Upvotes: 0

Related Questions