[英]JavaScript regex for password containing at least 6 characters, 1 number, 1 letter, any special characters
I am trying like this: 我正在这样尝试:
(/^(?=.*\\d)(?=.*[az])(?=.*[AZ]).{6,}$/)
but it is not working. 但它不起作用。
Your can use /^(?=.*\\d)(?=.*[a-zA-Z])(?=.*[!@#$%^&*()_+\\-=\\[\\]{};':"\\\\|,.<>\\/?]).{6,}$/
. 您可以使用
/^(?=.*\\d)(?=.*[a-zA-Z])(?=.*[!@#$%^&*()_+\\-=\\[\\]{};':"\\\\|,.<>\\/?]).{6,}$/
。
(?=.*\\d)
for at least one digit (?=.*\\d)
至少一位数字 (?=.*[a-zA-Z])
for at least one letter (?=.*[a-zA-Z])
至少一个字母 (?=.*[!@#$%^&*()_+\\-=\\[\\]{};':"\\\\|,.<>\\/?])
for at least one special character (?=.*[!@#$%^&*()_+\\-=\\[\\]{};':"\\\\|,.<>\\/?])
至少一个特殊字符 Use match()
to find pattern 使用
match()
查找模式
$('#text').keyup(function() { $(this).css('border', this.value.match(/^(?=.*\\d)(?=.*[a-zA-Z])(?=.*[!@#$%^&*()_+\\-=\\[\\]{};':"\\\\|,.<>\\/?]).{6,}$/) ? '5px solid green' : '5px solid red'); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <input type="text" id="text" />
Or you can also use test()
to find match 或者您也可以使用
test()
查找匹配项
$('#text').keyup(function() { var re = new RegExp(/^(?=.*\\d)(?=.*[a-zA-Z])(?=.*[!@#$%^&*()_+\\-=\\[\\]{};':"\\\\|,.<>\\/?]).{6,}$/); $(this).css('border', re.test(this.value) ? '5px solid green' : '5px solid red'); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <input type="text" id="text" />
You need to remove the parenthesis from around the opening and closing /
in your regular expression. 您需要在正则表达式中的开头和结尾
/
中删除括号。
Also, it is possible you wanted to combine the [az]
and [AZ]
into [a-zA-Z]
so that both upper and lower case letters don't have to found, just one of the two. 另外,您可能想将
[az]
和[AZ]
为[a-zA-Z]
这样就不必同时使用大写和小写字母,只需找到两者之一即可。
Try utilizing String.prototype.match
尝试利用
String.prototype.match
var str1 = "abc1e."; var str2 = "abc1e "; // match special character; any character not digit , // not any alphanumeric character , including `_` , // not space character var res1 = str1.match(/[^\\d|\\w|\\s]/i); // if match found , concat digit , alphanumeric character // if resulting array length is 6 , return `true` , else return `false` res1 = !!res1 ? res1[0].concat(str1.match(/\\d+|\\w+/i)).length === 6 : false; var res2 = str2.match(/[^\\d|\\w|\\s]/i); res2 = !!res2 ? res2[0].concat(str1.match(/\\d+|\\w+/i)).length === 6 : false; console.log(res1, res2);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.