简体   繁体   English

使用JavaScript进行正则表达式密码验证

[英]Regex password validation using JavaScript

I need to validate (and confirm) a new password on a site. 我需要验证(并确认)网站上的新密码。 There are criteria I need to test for, and I'm looking for the best method to do so, using jQuery. 有一些标准需要测试,我正在寻找使用jQuery的最佳方法。

The criteria are: 条件是:

  1. Must have a minimum of 8 characters and a maximum of 12 必须至少包含8个字符,最多12个字符
  2. Should have at least three of the following: A. Upper case letter; 应至少具有以下三个条件:A.大写字母; B. Lower case letter; B.小写字母; C. Numeric character; C.数字字符; D. Punctuation. D.标点符号。

EDIT: The following regexp is what I'm currently using to accomplish this. 编辑:下面的正则表达式是我目前正在使用来完成此操作。

regexp/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*(_|[^\w])).+$/

However, that checks to make sure that all four of those types are present, when I need only three out of the four (and it doesn't matter which three.) Can anyone suggest how to improve my regex to accomplish this? 但是,当我只需要四种中的三种(并且哪三种都不重要)时,可以进行检查以确保所有这四种类型都存在。有人可以建议如何改进我的正则表达式来实现这一点吗?

UPDATE : I've still not figured out how to accomplish this, but have thought of a few scenarios. 更新 :我仍然没有弄清楚如何完成此操作,但是考虑了一些情况。 The way I see it is that it should be done using 5 permutations, and using AND and OR operators. 我认为它应该使用5个置换以及AND和OR运算符完成。 However, I don't know how to do that in regex. 但是,我不知道如何在正则表达式中执行此操作。 Am I barking up the wrong tree? 我在树上叫错了吗?

好吧,经过一番尝试之后,下面是解决方法:

regexp/(^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*(_|[^\w])).+$)|(^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).+$)|(^(?=.*[a-z])(?=.*[A-Z])(?=.*(_|[^\w])).+$)|(^(?=.*[A-Z])(?=.*\d)(?=.*(_|[^\w])).+$)|(^(?=.*[a-z])(?=.*\d)(?=.*(_|[^\w])).+$)/

Here's a website which has a password validation program in javascript: 这是一个使用javascript进行密码验证的网站:

http://www.javascriptsource.com/forms/val-pass.html http://www.javascriptsource.com/forms/val-pass.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM