我有以下内容:
$('#qForm').validate({
rules: {
answerYesNo_1: { required:true } ,
answerYesNoText_1: { required: function(element) {
return $('#answerYesNo_1').val() == 'yes';
}
, isMoney: true },
answerYesNo_2: { required:true } ,
answerYesNoText_2: { required: function(element) {
return $('#answerYesNo_2').val() == 'yes';
}
, isMoney: true },
answerYesNo_3: { required:true } ,
answerYesNoText_3: { required: function(element) {
return $('#answerYesNo_3').val() == 'yes';
}
, isMoney: true },
answerYesNo_4: { required:true } ,
answerYesNoText_4: { required: function(element) {
return $('#answerYesNo_4').val() == 'yes';
}
, isMoney: true },
answerYesNo_5: { required:true } ,
answerYesNoText_5: { required: function(element) {
return $('#answerYesNo_5').val() == 'yes';
}
, isMoney: true }
},
...
jQuery.validator.addMethod("isMoney", function(value, element) {
alert(/^\$?[0-9][0-9\,]*(\.\d{1,2})?$|^\$?[\.]([\d][\d]?)$/.test(value));
return /^\$?[0-9][0-9\,]*(\.\d{1,2})?$|^\$?[\.]([\d][\d]?)$/.test(value);
});
我遇到的问题是忽略了所需功能的第一部分:
return $('#answerYesNo_1').val() == 'yes'
说:我为answerYesNo_2选择“no”,answerYesNo_3,answerYesNo_4,answerYesNo_5,我会为每个人选择false。如果答案是否定的,则不应该进入isMoney验证。有帮助吗?
答案 0 :(得分:0)
试试这个:
$.validator.addMethod("isMoney", function (value, element) {
alert(/^\$?[0-9][0-9\,]*(\.\d{1,2})?$|^\$?[\.]([\d][\d]?)$/.test(value));
return /^\$?[0-9][0-9\,]*(\.\d{1,2})?$|^\$?[\.]([\d][\d]?)$/.test(value);
});
$.validator.addMethod(
'equalToParam',
function ( value, element, param ) {
return $.trim(value) == param;
},
'Do not match.'
);
$('#qForm').validate({
rules: {
answerYesNo_1: {
required: true,
equalToParam: 'yes'
},
answerYesNoText_1: {
required: true,
isMoney: true
},
answerYesNo_2: {
required: true
},
answerYesNoText_2: {
required: true,
equalToParam: 'yes'
isMoney: true
},
answerYesNo_3: {
required: true
},
answerYesNoText_3: {
required: true,
equalToParam: 'yes'
isMoney: true
},
answerYesNo_4: {
required: true
},
answerYesNoText_4: {
required: true,
equalToParam: 'yes'
isMoney: true
},
answerYesNo_5: {
required: true
},
answerYesNoText_5: {
required: true,
equalToParam: 'yes'
isMoney: true
}
},
下次或如果我的代码不合作,请在http://jsfiddle.net/上进行演示