[英]Check a password in Javascript with a regular expression
i've got an expression checking whether there is at least one number, one lowercase and one uppercase letter and at least 6 characters in a password string. 我有一个表达式,检查密码字符串中是否至少有一个数字,一个小写字母和一个大写字母以及至少6个字符。 please see my example:
请看我的例子:
var myPwd = 'test-123H';
console.log('password to check: ' + myPwd);
//at least one number, one lowercase and one uppercase letter and at least six characters
var re = /(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,}/;
var validPassword = re.test(myPwd);
console.log(validPassword);
unfortunatly i can't manage it to achieve two more conditions: the password should be checked against a maximum length of 15 characters and there should be at least 2 numbers instead of one. 不幸的是,我无法管理它以实现另外两个条件:应该检查密码的最大长度为15个字符,并且应该至少有2个数字而不是1个。 could anyone tell me the trick?
谁有人能告诉我诀窍?
You can modify your regex to this: 你可以修改你的正则表达式:
var re = /^(?=(\D*\d){2})(?=.*?[a-z])(?=.*?[A-Z]).{6,15}$/;
(?=(\\D*\\d){2})
=> will make sure there are at least 2 digits (?=(\\D*\\d){2})
=>将确保至少有2位数字 .{6,15}
makes sure there are min 6 and max 15 character .{6,15}
确保最少6个字符和最多15个字符 ^ and $
are line start and line end anchors ^ and $
是行开始和行结束锚点 Visual Description: 视觉描述:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.