admin管理员组

文章数量:1277885

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 blank, 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?

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 blank, 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?

Share Improve this question edited Mar 8, 2013 at 9:01 j0k 22.8k28 gold badges81 silver badges90 bronze badges asked Aug 27, 2011 at 22:21 sixtwowaifusixtwowaifu 7974 gold badges17 silver badges41 bronze badges 2
  • 4 Oh great, still more falsehoods that deluded programmers believe about people’s names. They are just not true. Don’t make such silly assumptions about people’s names! Anyway [A-Z] is a screamingly loud antipattern; it’s “always wrong, sometimes” like now: William MᶜKinley, Martin O’Malley, King Henry Ⅷ, François Mitterand, Federico Peña, Nicolae Ceaușescu, Ævar Arnfjörð Bjarmason, and Dominique Gaston André Strauss-Kahn all laugh at your puny broken regex! – tchrist Commented Aug 27, 2011 at 23:20
  • Great sample! Also don't forget on all these Arabic, Georgian, Cyrillic, Japanese, Chinese and all other alphabets people are use... – Konstantin Isaev Commented Nov 2, 2014 at 19:02
Add a ment  | 

4 Answers 4

Reset to default 3

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;               
    }

本文标签: Regex letters only validation for javascript promptStack Overflow