正则表达式字母仅验证 javascript 提示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7217766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Regex letters only validation for javascript prompt
提问by cassidymack
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?
另外,我必须编写 if 语句来确保用户输入至少两个名字怎么办?即必须是 1 个字母 + 一个空格 + 1 个字母。建议?
回答by jtbandes
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.)
(或!name.match(alphaExp)
为否定。)
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.)
(我删除了 0-9,因为您可能不想要数字。)
回答by JJ.
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:
但是请注意,alphaExp
您允许使用数字/数字,这与您只想要字母的声明相反。如果您真的只想要字母,请alphaExp
按以下方式更改:
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:
至于您关于强制输入 2 个名字“姓氏”的问题,您可以使用以下内容:
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);
}
回答by mVChr
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:
要测试一个RegExp你可以使用正则表达式的方法test
,exec
或match
。对于您的情况test
将是最合适和最高效的:
if (!/^[a-zA-Z]+$/.test(name)) { alert("error"); }
To match AT LEAST two names, you would use this RegExp:
要匹配至少两个名称,您可以使用这个 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.
请注意,\-
因为有些人将姓名和第三个括号中的空格设置为连字符,以允许两个以上的姓名,但防止人们输入一个姓名后跟空格。
回答by balamurugan
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;
}