How do you create a simple, custom rule using the jQuery Validate plugin (using addMethod
) that doesn't use a regex?
For example, what function would create a rule that validates only if at least one of a group of checkboxes is checked?
Then to define rules use simple syntax. jQuery(document). ready(function() { jQuery("#forms). validate({ rules: { firstname: 'required', lastname: 'required', u_email: { required: true, email: true,//add an email rule that will ensure the value entered is valid email id.
The jquery validate plugin requires a form element to function, so you should have your form fields (no matter how few) contained inside a form. You can tell the validation plugin not to operate on form submission, then manually validate the form when the correct submit button is clicked.
validator. addMethod( "selectnic" function(value,element){ if(element. value == /^[0-9]{9}[vVxX]$/) return false; else return true; }, "wrong nic number" ); $('#basicDetails').
You can create a simple rule by doing something like this:
jQuery.validator.addMethod("greaterThanZero", function(value, element) { return this.optional(element) || (parseFloat(value) > 0); }, "* Amount must be greater than zero");
And then applying this like so:
$('validatorElement').validate({ rules : { amount : { greaterThanZero : true } } });
Just change the contents of the 'addMethod' to validate your checkboxes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With