简体   繁体   English

密码包含至少6个字符,1个数字,1个字母和任何特殊字符的JavaScript正则表达式

[英]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,}$/

  1. (?=.*\\d) for at least one digit (?=.*\\d)至少一位数字
  2. (?=.*[a-zA-Z]) for at least one letter (?=.*[a-zA-Z])至少一个字母
  3. (?=.*[!@#$%^&*()_+\\-=\\[\\]{};':"\\\\|,.<>\\/?]) 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.

相关问题 正则表达式 - 至少1个数字,1个字母,1个特殊字符和至少3个字符 - Regex - At least 1 number, 1 letter, 1 special character and at least 3 characters 最少 6 个字符的密码 REGEX,至少一个字母和一个数字,可以包含特殊字符 - Password REGEX with min 6 chars, at least one letter and one number and may contain special characters 密码的正则表达式必须至少包含八个字符,至少一个数字以及大小写字母和特殊字符 - Regex for password must contain at least eight characters, at least one number and both lower and uppercase letters and special characters 正则表达式:必须至少包含一个数字和字母,但不能包含其他字符/空格 - RegEx: Must have at least one number and letter but no other characters / whitespace 用于JavaScript的正则表达式,用于测试字符串,数字和特殊字符 - Regex for javascript to test strings, number and special characters 正则表达式在 JavaScript 中包含至少一个字母和至少 6 个字符长的所有字符 - Regex to include all characters with at least one letter and at least 6 characters long in JavaScript Javascript正则表达式,表示“至少X个字符,带有大写字母和数字” - Javascript regular expression for “at least X characters, have a capital letter and a number” Javascript正则表达式特殊字符 - Javascript regex special characters javascript 特殊字符的正则表达式 - javascript regex for special characters 以字母开头的正则表达式包含一个大写/一个小写字母,一个数字,没有特殊字符和最少8个字符 - Regex that starts with letter, contains one uppercase/one lowercase letter, one number and no special characters & min 8 characters
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM