我想知道如何在两个输入文本框中为不相等的值添加jquery验证。我已经尝试过此Example,但对我不起作用。
jQuery代码:
$.validator.addMethod("notEqual", function (value, element, param) {
return this.optional(element) || value != param;
}, "cannot be same as oldpassword");
$("#frmAddEdit").validate({
rules: {
"txtoldpassword": { required: true },
"txtnpassword": { required: true, notEqual:"#txtoldpassword" },
},
messages: {
"txtoldpassword": { required: "old Password is required" },
"txtnpassword": { required: "New Password is required" },
},
submitHandler: function (form) {
ChangeUserPassword();
return false;
}
});
当我在验证方法上放置警报时,param会显示相同的值(即#txtoldpassword
),而当我这样输入时,notEqual: $("#txtoldpassword").val()
则param会变成""
(空白)
请帮助我。
答案 0 :(得分:0)
我只是为notEqual更改了传递文本框值的方式,对我有用。
JQuery代码:
[2018-07-12 13:50:21] local.ALERT: 330: 330
[2018-07-12 13:50:21] local.ALERT: 0.00079900000002908
[2018-07-12 13:50:21] local.ALERT: 330.01: 330.01
[2018-07-12 13:50:21] local.ALERT: 0.0010900000000333
[2018-07-12 13:50:21] local.ALERT: 330.02: 330.02
[2018-07-12 13:50:21] local.ALERT: 0.0013809999999808
[2018-07-12 13:50:21] local.ALERT: 330.03: 330.03
[2018-07-12 13:50:21] local.ALERT: 0.001671999999985
[2018-07-12 13:50:21] local.ALERT: 330.04: 330.04
[2018-07-12 13:50:21] local.ALERT: 0.0019630000000461
[2018-07-12 13:50:21] local.ALERT: 330.05: 330.05