正则表达式的密码:QUOT; ATLEAST 1个字母,1个数字,特殊字符,不应以特殊字符&QUOT启动; [英] Regex for Password: "Atleast 1 letter, 1 number, 1 special character and SHOULD NOT start with a special character"

查看:715
本文介绍了正则表达式的密码:QUOT; ATLEAST 1个字母,1个数字,特殊字符,不应以特殊字符&QUOT启动;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个密码字段经常EX pression。

的要求是:


  1. 密码长度必须在8到20个字符


  2. 必须至少包含一个字母一个号码并从 特殊字符!@#$%^&放; *()_ +


  3. 不应该以特殊字符开始


我曾尝试

  ^(= * [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:

  1. The password Must be 8 to 20 characters in length

  2. Must contain at least one letter and one number and a special character from !@#$%^&*()_+.

  3. 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

Regex Demo

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启动;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆