jQuery Validation using the class instead of the name value

Matt :

I'd like to validate a form using the jquery validate plugin, but I'm unable to use the 'name' value within the html - as this is a field also used by the server app. Specifically, I need to limit the number of checkboxes checked from a group. (Maximum of 3.) All of the examples I have seen, use the name attribute of each element. What I'd like to do is use the class instead, and then declare a rule for that.

html

This works:

<input class="checkBox" type="checkbox" id="i0000zxthy" name="salutation"  value="1" />

This doesn't work, but is what I'm aiming for:

<input class="checkBox" type="checkbox" id="i0000zxthy" name="i0000zxthy"  value="1" />

javascript:

var validator = $(".formToValidate").validate({    
    rules:{     
    "salutation":{  
             required:true,  
        },  
        "checkBox":{  
             required:true,  
          minlength:3  }  
   }   
});

Is it possible to do this - is there a way of targeting the class instead of the name within the rules options? Or do I have to add a custom method?

Cheers, Matt

Nick Craver :

You can add the rules based on that selector using .rules("add", options), just remove any rules you want class based out of your validate options, and after calling $(".formToValidate").validate({... });, do this:

$(".checkBox").rules("add", { 
  required:true,  
  minlength:3
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Jquery get value using class name

Laravel custom validation rule. How to add possibility for passing rule with a string representation instead of using class name?

assign individual value to text field with same class name using jquery

How to get all of the input value using a class name in jQuery?

jQuery using variable name instead id name

Copy nearest select box value of a given class name to following select box of same class name (using JQuery)

jquery put a value instead of selector name

Get class name using jQuery

Why Use Class Methods Instead Of Just Using The Class Name For Instance?

How to use multiple input field class name in jquery for validation

Dynamic class name from hash value with jquery

jQuery Getting SPAN value with a specific Class name

How to get value from clicked radio button by refer its class name using jquery?

using nameof() to inspect class name with its parent for MVC validation

Use Class “Name” Instead of “Name”

JQuery UI autocomplete combobox selecting value instead of name

Using variable value as dictionary/class name in Python

Get Class Name Value Using "Starts With" Selector

using a variable value on css modules class name

Select class name (number) using RegEx & Jquery

How to use ID or CLASS instead of using name in Bootstrap Validator

Using the name of a select box instead of id or class in an event

How to validate input fields with a dot in name using the jquery validation plugin?

Laravel validation find input name with JQuery using dot array syntax

How to validate name="novs[78][] using jquery validation?

How to hide element using Class; instead of using css function in JQuery?

Display the name instead of the value

change value of name attribute using jquery

How to get value of textbox in jquery using name?