admin管理员组文章数量:1023764
I am trying to validate a password string with javascript and need some help with a regex. I have tried some tutorials, but I think I have some problems understanding how to escape quantifiers and/or metacharacters.
I want to make sure that the password string only contains one or more (max 32) characters from the following spans:
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"012345678901234567890123456789"
"!@#%&/(){}[]=?+*^~-_.:,;"
The first three spans are pretty easy, but I can't figure out the last one. Basically my script looks something like this:
var password = "user_input_password";
if (/^[A-Za-z0-9!@#$%...]{1,32}$/.test(password)) {
document.write('OK');
} else {
document.write('Not OK');
}
Any help or input is highly appreciated, thanks!
I am trying to validate a password string with javascript and need some help with a regex. I have tried some tutorials, but I think I have some problems understanding how to escape quantifiers and/or metacharacters.
I want to make sure that the password string only contains one or more (max 32) characters from the following spans:
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"012345678901234567890123456789"
"!@#%&/(){}[]=?+*^~-_.:,;"
The first three spans are pretty easy, but I can't figure out the last one. Basically my script looks something like this:
var password = "user_input_password";
if (/^[A-Za-z0-9!@#$%...]{1,32}$/.test(password)) {
document.write('OK');
} else {
document.write('Not OK');
}
Any help or input is highly appreciated, thanks!
Share Improve this question asked Nov 26, 2011 at 7:55 JoséJosé 3913 silver badges14 bronze badges3 Answers
Reset to default 4In general, you can escape a meta-character using a backslash \
; however, inside a character class, the only ones you have to escape are ]
, \
and -
(the ^
only has a meaning at the very beginning). Something like [\w!@#%&/(){}[\]=?+*^~\-.:,;]
will do what you want.
The \w
is equal to [A-Za-z0-9_]
.
So the full test would be something like:
/^[\w!@#%&/(){}[\]=?+*^~\-.:,;]{1,32}$/.test(password)
/^[A-Za-z0-9!@#%&\/(){}\[\]=?+*^~\-_\.:,;]{1,32}$/
You can also match all characters that are not considered white space (space, newline, tab)
/^[^\s]{1,32}$/.test(password);
To exclude quotes as well (I didn't see them in your example) you can add those in:
/^[^\s'"]{1,32}$/.test(password);
I am trying to validate a password string with javascript and need some help with a regex. I have tried some tutorials, but I think I have some problems understanding how to escape quantifiers and/or metacharacters.
I want to make sure that the password string only contains one or more (max 32) characters from the following spans:
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"012345678901234567890123456789"
"!@#%&/(){}[]=?+*^~-_.:,;"
The first three spans are pretty easy, but I can't figure out the last one. Basically my script looks something like this:
var password = "user_input_password";
if (/^[A-Za-z0-9!@#$%...]{1,32}$/.test(password)) {
document.write('OK');
} else {
document.write('Not OK');
}
Any help or input is highly appreciated, thanks!
I am trying to validate a password string with javascript and need some help with a regex. I have tried some tutorials, but I think I have some problems understanding how to escape quantifiers and/or metacharacters.
I want to make sure that the password string only contains one or more (max 32) characters from the following spans:
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"012345678901234567890123456789"
"!@#%&/(){}[]=?+*^~-_.:,;"
The first three spans are pretty easy, but I can't figure out the last one. Basically my script looks something like this:
var password = "user_input_password";
if (/^[A-Za-z0-9!@#$%...]{1,32}$/.test(password)) {
document.write('OK');
} else {
document.write('Not OK');
}
Any help or input is highly appreciated, thanks!
Share Improve this question asked Nov 26, 2011 at 7:55 JoséJosé 3913 silver badges14 bronze badges3 Answers
Reset to default 4In general, you can escape a meta-character using a backslash \
; however, inside a character class, the only ones you have to escape are ]
, \
and -
(the ^
only has a meaning at the very beginning). Something like [\w!@#%&/(){}[\]=?+*^~\-.:,;]
will do what you want.
The \w
is equal to [A-Za-z0-9_]
.
So the full test would be something like:
/^[\w!@#%&/(){}[\]=?+*^~\-.:,;]{1,32}$/.test(password)
/^[A-Za-z0-9!@#%&\/(){}\[\]=?+*^~\-_\.:,;]{1,32}$/
You can also match all characters that are not considered white space (space, newline, tab)
/^[^\s]{1,32}$/.test(password);
To exclude quotes as well (I didn't see them in your example) you can add those in:
/^[^\s'"]{1,32}$/.test(password);
本文标签: Javascript regex validate password string (escaping punctuations)Stack Overflow
版权声明:本文标题:Javascript regex validate password string (escaping punctuations) - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745541345a2155196.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论