Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Validate Plugin - How to create a simple custom rule?

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?

like image 917
Edward Avatar asked Oct 27 '08 19:10

Edward


People also ask

What is validation write example of simple jQuery form?

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.

Does jQuery validate require a form?

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.

How can call Add method in jQuery validation?

validator. addMethod( "selectnic" function(value,element){ if(element. value == /^[0-9]{9}[vVxX]$/) return false; else return true; }, "wrong nic number" ); $('#basicDetails').


1 Answers

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.

like image 97
Mark Spangler Avatar answered Oct 21 '22 04:10

Mark Spangler