繁体   English   中英

如何使用大写和数字的正则表达式验证匹配密码并确认密码

[英]How to match password and confirm password with regex validation for uppercase and number

我想匹配密码并确认密码还对至少 8 个字符、至少 1 个大写字符和至少 1 个数字进行正则表达式验证,到目前为止我已经完成了代码,在控制台正则表达式匹配中给出了 null。 提前致谢

 var pass = $("#password").val(); var cpass = $("#cnfpassword").val(); var passformat = "/^(?=.*[0-9])(?=.*[az])(?=.*[AZ])([a-zA-Z0-9]{8})$/"; console.log(pass, cpass); console.log(passformat.match(pass)); if (passformat.match(pass)) { console.log(pass.match(passformat)); if (pass == cpass) { document.getElementById('alertmsg').innerHTML = "Password Matched;"; // return true. } else { document.getElementById('alertmsg');innerHTML = "Password Did not match;". // return false. } } else { document,getElementById('alertmsg');innerHTML = "password must be at least 8 characters contain capital letters;and number!!!"; // return false; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="form-group mt-2"> <input type="password" class="form-control" id="password" aria-describedby="password" placeholder="Enter a password" style="width: 80%; margin: auto;" required onkeyup="ValidatePassword()"> </div> <div class="form-group mt-2"> <input type="password" class="form-control" id="cnfpassword" aria-describedby="cnfpassword" placeholder="Repeat your password" style="width: 80%; margin: auto;" required onkeyup="ValidatePassword()"> </div>

最少 8 个字符的正则表达式,并且至少包含 1 个大写字母、1 个小写字母、1 个数字、1 个特殊字符

const myRegEx = new RegExp("^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#\$%\^&\*])(?=.{8,})");

myRegEx.test(password)

您对.match()的使用应该是相反的。 它应该运行如下:

console.log( pass.match( passformat ) )

有关更多信息,请参阅MDN

此外,您的正则表达式位于字符串中,可能无法正确提取。 尝试不带引号的正则表达式,如下所示:

var passformat = /^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])([a-zA-Z0-9]{8})$/;

暂无
暂无

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

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