问题描述
我需要一个密码字段经常EX pression。
的要求是:
-
密码长度必须在8到20个字符
-
必须至少包含一个字母和一个号码并从
特殊字符!@#$%^&放; *()_ +
。 -
不应该以特殊字符开始
我曾尝试
^(= * [A-ZA-Z]。)(= * [@#$%^&AMP(= * \\ D?);?!*( )_ +])[A-ZA-Z \\ D @#$%^&安培;!*()_ +] {8,20}
它的工作原理,但如何从开始的口令限制的特殊字符?此外,如果你有一个更有效的正则表达式比上面提到的请建议的人。
感谢您
它的简单,只需要在开始时增加一个字符类
<$p$p><$c$c>^(?=.*[a-zA-Z])(?=.*\\d)(?=.*[!@#$%^&*()_+])[A-Za-z\\d][A-Za-z\\d!@#$%^&*()_+]{7,19}$-
[A-ZA-Z \\ D]
确保第一个字符是字母或数字。 -
[A-ZA-Z \\ D @#$%^&安培;!*()_ +] {7,19}
将匹配最低7最大19字符。作为他presceding字符类将消耗单个字符使得字符串作为最低8和最大20个字符的总数,这是必需 -
$
锚在字符串末尾的正则表达式。确保有没有按照我们的的有效的密码的
VAR模式=新RegExp(/^(?=.*[a-zA-Z])(?=.*\\d)(?=.*[!@#$%^&*()_+])[A-Za-z\\d][A-Za-z\\d!@#$%^&*()_+]{7,19}$/);的console.log(pattern.test(@#123asdf @#!));
= GT;假的console.log(pattern.test(123asdf @#!));
= GT;真正的console.log(pattern.test(12AS#));
= GT;假
I need a regular expression for a password field.
The requirement is:
The password Must be 8 to 20 characters in length
Must contain at least one letter and one number and a special character from
!@#$%^&*()_+
.Should not start with a special character
I have tried
^(?=.*[a-zA-Z])(?=.*\d)(?=.*[!@#$%^&*()_+])[A-Za-z\d!@#$%^&*()_+]{8,20}
It works but how do you restrict special characters from beginning the password? Also if you have a more efficient regex than the one mentioned above please suggest.
Thank you
Its simple, just add one more character class at the begining
^(?=.*[a-zA-Z])(?=.*\d)(?=.*[!@#$%^&*()_+])[A-Za-z\d][A-Za-z\d!@#$%^&*()_+]{7,19}$
[A-Za-z\d]
Ensures that the first character is an alphabet or digit.[A-Za-z\d!@#$%^&*()_+]{7,19}
will match minimum 7 maximum 19 character. This is required as he presceding character class would consume a single character making the total number of characters in the string as minimum 8 and maximum 20.$
Anchors the regex at the end of the string. Ensures that there is nothing following our valid password
var pattern = new RegExp(/^(?=.*[a-zA-Z])(?=.*\d)(?=.*[!@#$%^&*()_+])[A-Za-z\d][A-Za-z\d!@#$%^&*()_+]{7,19}$/);
console.log( pattern.test("!@#123asdf!@#") );
=> false
console.log( pattern.test("123asdf!@#") );
=> true
console.log( pattern.test("12as#") );
=> false
这篇关于正则表达式的密码:QUOT; ATLEAST 1个字母,1个数字,特殊字符,不应以特殊字符&QUOT启动;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!