[英]Javascript password validation using regex
I have a problem with my password validation using regex on javascript. 我在javascript上使用正则表达式进行密码验证时遇到问题。 the criteria are :
标准是:
I hope anyone can help me to solve this problem :) 我希望任何人都可以帮助我解决这个问题:)
var ch_pass = /^([0-9]+).([a-zA-Z]+).({3,30})$/;
您可以像这样使用前瞻:
var ch_pass = /^(?=.*?[0-9])(?=.*?[a-zA-Z]).{3,30}$/;
I wouldn't recommend trying to do this whole check in a single regular expression because it just over complicates it. 我不建议尝试在单个正则表达式中进行整个检查,因为这会使它过于复杂。 Do each condition individually.
分别执行每个条件。
Has at least one letter: 至少有一个字母:
var has_letters = (/[a-zA-Z]/).test(password);
Has at least one number: 至少有一个数字:
var has_numbers = (/[0-9]/).test(password);
Has between 3 and 30 characters (inclusive): 包含3到30个字符(包括3个字符):
var has_length = 3 <= password.length && password.length <= 30;
This can all be wrapped up into a function: 所有这些都可以包装成一个函数:
function is_password_valid(password) {
var has_letters = (/[a-zA-Z]/).test(password);
var has_numbers = (/[0-9]/).test(password);
var has_length = 3 <= password.length && password.length <= 30;
return has_letters && has_numbers && has_length;
}
Or if you prefer something more dense: 或者,如果您更喜欢稠密的东西:
function is_password_valid(password) {
return ((/[a-zA-Z]/).test(password)
&& (/[0-9]/).test(password)
&& password.length >= 3
&& password.length <= 30);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.