Reputation: 143
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
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