正则expression式用于密码validation

任何人都可以帮我创build一个正则expression式来进行密码validation。

条件是“密码必须包含8个字符和至less一个数字,一个字母和一个独特的字符,如!#$%&? "

 ^.*(?=.{8,})(?=.*[a-zA-Z])(?=.*\d)(?=.*[!#$%&? "]).*$ --- ^.* : Start (?=.{8,}) : Length (?=.*[a-zA-Z]) : Letters (?=.*\d) : Digits (?=.*[!#$%&? "]) : Special characters .*$ : End 

尝试这个

 ((?=.*\d)(?=.*[az])(?=.*[AZ])(?=.*[\W]).{6,20}) 

以上正则expression式的描述:

 ( # Start of group (?=.*\d) # must contains one digit from 0-9 (?=.*[az]) # must contains one lowercase characters (?=.*[\W]) # must contains at least one special character . # match anything with previous condition checking {8,20} # length at least 8 characters and maximum of 20 ) # End of group 

“/ W”将增加可用于密码的字符的范围,并且坑可以更安全。

您可以很容易地达到每个个人需求(例如,最less8个字符: .{8,}将匹配8个或更多个字符)。

要合并它们,您可以使用“积极向前”将多个子expression式应用于相同的内容。 类似(?=.*\d.*).{8,}来匹配一个(或多个)数字与超前,以及8个或更多的字符。

所以:

 (?=。* \ d *)(?=。* [A-ZA-Z]。*)(?=。* [!#\ $%&\?]。*)。{8,}

记住要转义元字符。

具有以下条件的密码:

  1. 至less1位数字
  2. 至less2个特殊字符
  3. 至less1个字母字符
  4. 没有空白

     'use strict'; (function() { var foo = '3g^g$'; console.log(/^(?=.*\d)(?=(.*\W){2})(?=.*[a-zA-Z])(?!.*\s).{1,15}$/.test(foo)); /** * (?=.*\d) should contain at least 1 digit * (?=(.*\W){2}) should contain at least 2 special characters * (?=.*[a-zA-Z]) should contain at least 1 alphabetic character * (?!.*\s) should not contain any blank space */ })(); 

你可以使你自己的正则expression式的javascriptvalidation;

  (/^ (?=.*\d) //should contain at least one digit (?=.*[az]) //should contain at least one lower case (?=.*[AZ]) //should contain at least one upper case [a-zA-Z0-9]{8,} //should contain at least 8 from the mentioned characters $/) 

例如: – /^(?=.*\d)(?=.*[a-zA-Z])[a-zA-Z0-9]{7,}$/