Multiple Validation + $.validator on single field
How can call multiple validation functions on single field in jQuery.
as below
Multiple Validation on line:
JobDayofWeek: { isValidJOW: true }, JobDayofWeek: { regex: "^[1-7]$|^(\s*[1-7]+\s*,\s*)+[1-7]$" },
var isValidJOW = function isJOWValid(value) {
var newTemp = getDataforNewTemplate();
if (newTemp.AutoResubmitFlag == 'N' && value == '') {
return true;
}
else {
return false;
}
}
$.validator.addMethod("isValidJOW", function (value, element) {
var newTemp = getDataforNewTemplate();
return isValidJOW(newTemp.JobDayofWeek);
}, 'Job Day of Week Mandatory when auto resubmit flag is set to Y');
$.validator.addMethod("regex", function (value, element, regexp) {
var check = false;
var re = new RegExp(regexp);
return this.optional(element) || re.test(value);
}, "The list of values 1(Sunday) to 7 (Saturday). For multiples enter the following 1,2,3...");
$(document).ready(function () {
$("#maintTempForm").validate({
rules: {
JobTypes: { isValidJobType: true },
ExecutableName: { required: true },
ExecutableType: { isValidJExecutableType: true },
JobDayofWeek: { isValidJOW: true },
JobDayofWeek: { regex: "^[1-7]$|^(\s*[1-7]+\s*,\s*)+[1-7]$" },
JobFrequency: { regexJobFrequency: "^[1-365]$" },
AutoResubmitFlag: { isValidFlag: true }
},
messages: {
开发者_如何学C ExecutableName: "Please Enter Valid Executable Name"
}
});
});
This is always validating only the first one.
The rules:
rules: {
//...
JobDayofWeek: { isValidJOW: true },
JobDayofWeek: { regex: "^[1-7]$|^(\s*[1-7]+\s*,\s*)+[1-7]$" }
//...
}
are just an object literal and as such, cannot have multiple values for a single field. The JavaScript parser will only use one of the JobDayofWeek
values when building the literal, which one it will use is probably implementation defined but maybe not.
What happens with this rule set?
rules: {
JobTypes: { isValidJobType: true },
ExecutableName: { required: true },
ExecutableType: { isValidJExecutableType: true },
JobDayofWeek: { isValidJOW: true, regex: "^[1-7]$|^(\s*[1-7]+\s*,\s*)+[1-7]$" },
JobFrequency: { regexJobFrequency: "^[1-365]$" },
AutoResubmitFlag: { isValidFlag: true }
},
All I did was combine the two JobDayofWeek
values into one. That should work.
精彩评论