I am using jQuery validate plugin to validate the form data at the input point. For most of the pages, the validation is standards where I accept any non-empty value. However, there are forms in some pages which require specific validation (e.g. a pattern match)
I have included the generic validation function in the templates and the validation happens as expected. But I am finding it difficult to figure out how to extend
the above functions only for specific pages by adding an additional lines of code, giving the main function the capability to perform page-specific validations. I have checked 1, 2 3 and many other answers and had no luck. I do not need to replace the entire validation function, but to add specific parameters based on page.
This question was not helpful since I am calling onclick
function and NOT a simple function. What am I missing?
My main validation function is below
jQuery(document).ready(function() {
if(jQuery().validate) {
jQuery('#maindataform').validate({
ignore: ":hidden:not(select)",
errorClass: "is-invalid",
validClass: "is-valid",
errorPlacement: function(error, element) {
error.insertAfter(element);
},
//add rules parameters here
//add message parameters here
});
jQuery('#maindataformbtn').click(function() {
var formstatus = jQuery('#maindataform').valid();
if (formstatus) {
//alert ("valid");
jQuery('#maindataform').submit();
}
});
}
});
Page-specific parameters I am trying to add
rules: {
nic: {
pattern: "^[0-9]{9}(V|X)$)|(^[0-9]{12}$"
},
},
messages: {
nic: {
pattern: "NIC can have 9 numbers with 'V' or 12 numbers only"
},
}