lena lena - 21 days ago 7
jQuery Question

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 ?

Answer

You may use the regex

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

or

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