Regex letters only validation for javascript prompt
I have a page where a prompt window pops up and prompts the user for their first and last name. I'm trying to write validation for the prompt window. hitting cancel or leaving blank is working fine, but what I'm stuck on is ensuring the user enters letters only. Below is what I have.
var alphaExp = /^[0-9a-zA-Z]+$/;
var name=prompt("Enter your first and last name please.","");
if (name==null || name=="")
{
alert("First and last name must be filled out!");
location.reload(true);
}
else if (name==alphaExp)
{
alert("Name must contain letters only!")
location.reload(true);
}
If I hit cancel or leave blan开发者_如何学Ck, I'm alerted that the field must be filled out and the page refreshes, but if I enter a number or special character it accepts it and takes me to the page, which it shouldn't.
I tried changing
else if (name==alphaExp)
to
else if (name!=alphaExp)
but that seemed to give the opposite effect of what I wanted and accepted nothing.
What am I missing here?
Also, what if statement would I have to write to ensure the user enters at least two names? i.e. must be 1 letter + a space + 1 letter. suggestions?
You need
else if (!name.match(alphaExp))
However note that in alphaExp
you are allowing numbers/digits which is contrary to your statement that you want letters only. If you truly want only letters, change your alphaExp
as follows:
var alphaExp = /^[a-zA-Z]+$/;
Also just a suggestion, however note that many surnames (and some first names) use hyphens (-) and single quotes ('). You may want to allow these characters as well.
As for your question about enforcing that 2 names 'First Lastname' be entered, you can use something like this:
var alphaExp = /^[a-zA-Z]+ [a-zA-Z]+$/;
Your revised code would now be:
var alphaExp = /^[a-zA-Z]+ [a-zA-Z]+$/;
var name=prompt("Enter your first and last name please.","");
if (name==null || name=="")
{
alert("First and last name must be filled out!");
location.reload(true);
}
else if (!name.matches(alphaExp))
{
alert("Name must contain letters only!")
location.reload(true);
}
You have to test if the string matches the regex, not just if it's equal. Instead of
name==alphaExp
you should use
name.match(alphaExp)
(Or !name.match(alphaExp)
for the negative.)
If you want to force people to enter two names, you might use something like
/^[a-zA-Z]+ [a-zA-Z]+$/
(I removed the 0-9 because you probably don't want numbers.)
To test a RegExp you would use the RegExp methods test
, exec
, or match
. For your situation test
would be the most appropriate and performant:
if (!/^[a-zA-Z]+$/.test(name)) { alert("error"); }
To match AT LEAST two names, you would use this RegExp:
/^[a-zA-Z\-]+ [a-zA-Z]+[a-zA-Z\- ]+$/
Notice the \-
because some people have hyphenated names and the space in the third bracket set to allow more than two names but prevent people entering one name followed by spaces.
To accept only letters that are small or capital, no matter we can use
var ffirst_name=/^[a-zA-Z]+$/
if (!ffirst_name.test($first_name)) {
me('input.first_name').after('<span class="err">Please input a valid name!</span>').focus();
return false;
}
精彩评论