我需要检查我的密码是否包含一个数字和一个字符。
输入表单
<input class="form-control" data-validate="required,minlength[8],alphaNumeric" id="password" name="password" placeholder="password" type="text">
下面是我的 jQuery 验证方法。
jQuery.validator.addMethod("alphaNumeric", function (value, element) {
return this.optional(element) || /^[0-9a-zA-Z]+$/.test(value);
}, "password must contain atleast one number and one character");
问题是,上面的正则表达式没有验证 password
的条件。应至少包含一个数字和一个字符。
最佳答案
您可以像这样使用前瞻正则表达式来确保它与至少包含 1 个数字和 1 个字母的输入匹配:
/^(?=\D*\d)(?=[^a-z]*[a-z])[0-9a-z]+$/i
关于jquery - 密码应至少包含一个数字和一个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25604182/