Javascript 仅在javascript中大写字母的正则表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8411809/
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
Regular expression for upper case letter only in javascript
提问by Rohan Kumar
How can I validate a field only with upper case letters only alphabetic.Any word made by A-Z characters only...
如何仅使用大写字母仅使用字母来验证字段。仅由 AZ 字符构成的任何单词...
回答by flesk
Try something like this for the javascript validation:
尝试这样的 javascript 验证:
if (value.match(/^[A-Z]*$/)) {
// matches
} else {
// doesn't match
}
And for validation on the server side in php:
并用于在 php 中的服务器端验证:
if (preg_match("/^[A-Z]*$/", $value)) {
// matches
} else {
// doesn't match
}
It's always a good idea to do an additional server side check, since javascript checks can be easily bypassed.
进行额外的服务器端检查总是一个好主意,因为可以轻松绕过 javascript 检查。
回答by Paul
var str = 'ALPHA';
if(/^[A-Z]*$/.test(str))
alert('Passed');
else
alert('Failed');
回答by Irvin Dominin
Try this:
尝试这个:
if (<value>.match(/^[A-Z]*$/)) {
// action if is all uppercase (no lower case and number)
} else {
// else
}
here is a fiddle: http://jsfiddle.net/WWhLD/
这是一个小提琴:http: //jsfiddle.net/WWhLD/