Javascript 使用所有特殊字符验证字母数字值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10456315/
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
Validating Alpha-Numeric values with all Special Characters
提问by Abbas
i want to validate my text field with below:
1. alpha-numeric
2. And all special characters
i am not good in regex can anyone help me out creating a regex for above things.
我想用以下内容验证我的文本字段:
1. 字母数字
2.
我不擅长正则表达式的所有特殊字符任何人都可以帮助我为上述内容创建正则表达式。
回答by Stefan Dochow
alphanumeric Strings are matched like this:
字母数字字符串匹配如下:
^[a-zA-Z0-9]+$
It matches any string that only contains of the listed chars and is at least one char long.
它匹配任何只包含列出的字符且长度至少为一个字符的字符串。
With special chars it would work the same way.
使用特殊字符,它的工作方式相同。
But what do you consider to be a special char?
但是您认为什么是特殊字符?
For !@#$%^&*()+=-[]\';,./{}|":<>? – being the set of special chars, the regex would look like this:
对于 !@#$%^&*()+=-[]\';,./{}|":<>? – 作为一组特殊字符,正则表达式如下所示:
^[@!#$\^%&*()+=\-\[\]\\';,\.\/\{\}\|\":<>\? ]+$
Again, all the allowed characters are listed. The ones used within regexes as commands or quantifiers have to be escaped with a \
.
同样,列出了所有允许的字符。在正则表达式中用作命令或量词的那些必须用\
.
回答by Fabrício Matté
This will do what you want.
这会做你想做的。
function validate()
{
var val = <my string>;
if (val == '')
alert('String is empty!');
else if (!val.match(/[_\W]/))
alert('String contains only A-Z a-z 0-9 characters!');
else if (!val.match(/^\w[@!#$\^%&*()+=\-\[\]\\';,\.\/\{\}\|\":<>\?]/))
alert('String contains your predefined characters only!');
}
Note that both regexes work on double-negation, returning false in the first match of an illegal character for best performance. First is the negation of the \W
charset which is the negation of \w
. Second is a negation !
of the negation ^
of the pre-defined characters (\w
+ pre-defined chars). Reply if you want any explanation or modifications.
请注意,两个正则表达式都适用于双重否定,在非法字符的第一次匹配中返回 false 以获得最佳性能。首先是\W
字符集的否定,它是 的否定\w
。其次是否定预定义字符(+ 预定义字符)!
的否定。如果您需要任何解释或修改,请回复。^
\w
EDITHere's a regex to match if the string has at least one special character and alpha-numeric characters.
编辑如果字符串至少有一个特殊字符和字母数字字符,则这是一个匹配的正则表达式。
if (val.match(/[^_\W]/) && val.match(/[@!#$\^%&*()+=\-\[\]\\';,\.\/\{\}\|\":<>\? ]/))
alert('String contains both alpha-numeric and your pre-defined special characters!');
Is it ok or you need it in a single regex pattern?
可以吗,或者您需要在单个正则表达式模式中使用它?
EDITThis will do it in a single regex:
编辑这将在一个正则表达式中完成:
if (val.match(/(?=.*[@!#$\^%&*()+=\-\[\]\\';,\.\/\{\}\|\":<>\? ]+?).*[^_\W]+?.*/)
alert('String contains both alpha-numeric and your pre-defined special characters!');
回答by Mukesh
You can try this for all special characters
您可以为所有特殊字符尝试此操作
/^[0-9a-zA-Z\s\r\n@!#$\^%&*()+=\-\[\]\\';,\.\/\{\}\|\":<>\?]+$/;
回答by Tjcool
If want to allow specific special characters with alpha numeric then following regexp will work.You can customize, allowed special characters range as per your requirement.In case of escape characters you need to put in between \ \. In below example \-\ allows '-'.
如果想允许带有字母数字的特定特殊字符,则遵循正则表达式将起作用。您可以根据您的要求自定义允许的特殊字符范围。如果转义字符,您需要在 \ \ 之间放入。在下面的示例中 \\-\ 允许使用“-”。
/^[a-zA-Z0-9?=.*!@#$%^&*_\-\s]+$/
Hope this will help you :).
希望能帮到你 :)。
回答by Arbaz Shaikh
For the alphanumeric and all special characters Validation
对于字母数字和所有特殊字符验证
you can use in one regex
您可以在一个正则表达式中使用
This will return true if you have Minimum 8 characters at least 1 Uppercase Alphabet, 1 Lowercase Alphabet, 1 Number and 1 Special Character
如果您有至少 8 个字符,至少 1 个大写字母、1 个小写字母、1 个数字和 1 个特殊字符,这将返回 true
NSString *alphaNumberandSpecialRegex =@"^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}$";
NSPredicate *alphaNumberTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", alphaNumberandSpecialRegex];
return [alphaNumberTest evaluateWithObject:@"yourString"];
Minimum 8 characters at least 1 Alphabet and 1 Number:
最少 8 个字符,至少 1 个字母和 1 个数字:
NSString *alphaNumberandSpecialRegex =@""^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$"";
NSPredicate *alphaNumberTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", alphaNumberandSpecialRegex];
return [alphaNumberTest evaluateWithObject:@"yourString"];
Minimum 8 characters at least 1 Alphabet, 1 Number and 1 Special Character:
最少 8 个字符,至少 1 个字母、1 个数字和 1 个特殊字符:
NSString *alphaNumberandSpecialRegex =@"^(?=.*[A-Za-z])(?=.*\d)(?=.*[$@$!%*#?&])[A-Za-z\d$@$!%*#?&]{8,}$";
NSPredicate *alphaNumberTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", alphaNumberandSpecialRegex];
return [alphaNumberTest evaluateWithObject:@"yourString"];
Minimum 8 characters at least 1 Uppercase Alphabet, 1 Lowercase Alphabet and 1 Number:
最少 8 个字符,至少 1 个大写字母、1 个小写字母和 1 个数字:
NSString *alphaNumberandSpecialRegex =@"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,}$";
NSPredicate *alphaNumberTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", alphaNumberandSpecialRegex];
return [alphaNumberTest evaluateWithObject:@"yourString"];
Minimum 8 and Maximum 10 characters at least 1 Uppercase Alphabet, 1 Lowercase Alphabet, 1 Number and 1 Special Character:
最少 8 个和最多 10 个字符,至少 1 个大写字母、1 个小写字母、1 个数字和 1 个特殊字符:
NSString *alphaNumberandSpecialRegex =@"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&]{8,10}";
NSPredicate *alphaNumberTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", alphaNumberandSpecialRegex];
return [alphaNumberTest evaluateWithObject:@"yourString"];