我一直在阅读和使用 JQuery 中使用的正则表达式,并且部分工作正常,但似乎没有跟踪失败的正则表达式。
我认为,具体问题与“向后看”有关。
感谢 CodeJockey 的原始方向。这是我更新的代码:
<script type="text/javascript">
jQuery(document).ready(function($) {
$.validator.addMethod("lccharReqs", function(value, element) {
var lc = /(?=.*[a-z])/;
return this.optional(element) || lc.test(value);
}, "Your password should contain at least one lowercase letter!");
$.validator.addMethod("uccharReqs", function(value, element) {
var uc = /(?=.*[A-Z])/;
return this.optional(element) || uc.test(value);
}, "Your password should contain at least one uppercase letter!");
$.validator.addMethod("nccharReqs", function(value, element) {
var nc = /(?=.*[0-9])/;
return this.optional(element) || nc.test(value);
}, "Your password should contain at least one number!");
$.validator.addMethod("speccharReqs", function(value, element) {
var sc = /(?<=.*[\!\@\#\$\%\&\?\-\_])/;
return this.optional(element) || sc.test(value);
}, "Your password should contain at least one special character!");
$("#new_noisuf_pwd").validate({
rules: {
inf_field_Password: {
required: true,
minlength: 8,
lccharReqs: true,
uccharReqs: true,
nccharReqs: true,
speccharReqs: true
},
pwd_confirm : {
equalTo: "#inf_field_Password"
}
},
messages: {
inf_field_Password: {
required: "Please enter a password!",
minlength: "Your password must be at least 8 characters!",
maxlength: "Your password cannot be more the 12 characters!"
},
pwd_confirm : {
equalTo: "Your passwords does not match!"
}
}});
});
</script>
我倾向于“向后看”的原因是:
输入 aaaaaaaa - 我收到大写错误...输入 Aaaaaaaa - 我收到数字错误...输入 1Aaaaaaa - 我收到特殊字符错误,但 Enter Passw0rd - 我没有收到错误。
真的想学习这个并且已经阅读了很多,但我被困住了。
谢谢!
[原始问题] 我正在使用“验证”插件并添加如下方法:
jQuery.validator.addMethod("charReqs", function(value, element) {
return this.optional(element) || /^(([a-z]{1})([A-Z]{1})([0-9]{1})([\!\@\#\+\-]{1})) *$/.test(value);
}, "One lowercase letter, one capital letter, one number and one of the characters listed above - please!");
1.) 我是否有正确的语法来检查字符串中某处的特殊字符列表中的 1 个 lc char、1 个 uc char、1 个 num 和 1 个:
/^ ( ([a-z]{1}) ([A-Z]{1}) ([0-9]{1}) ([\!\@\#\+\-]{1}) ) *$/
2.)我是否正确分组?3.) 如果不是,请解释。
谢谢