0

I want to validate radio button by jquery validation plugin but the problem is jquery validation plugin use name for validation rule but i need class instead of name

here is my code

<input type="radio" name="brand[]" value="blue"> Blue<br />
    <input type="radio" name="brand[]" value="red"> Red<br />
    <input type="radio" name="brand[]" value="green"> Green<br />
    <label for="brand[]" class=brand style="display:none;">Please choose one.</label>

and my jquery

rules: {
                'brand[]':{ required:true }
            }

it's working fine with name attribute but i want to use class instead of name.

kazi
  • 77
  • 14
  • [https://jqueryvalidation.org/rules/](https://jqueryvalidation.org/rules/) You can add the rules by specific selector – user2970115 Jul 03 '17 at 08:49
  • You could put `required="required"` HTML5 attribute on the `input`, or you could just put `class="required"` and the plugin will pick up both automatically. – Sparky Jul 05 '17 at 15:35

1 Answers1

0

You can add the rules based on that selector using .rules("add", options)

<input type="radio" name="brand[]" value="blue" class="brand"> Blue<br />
    <input type="radio" name="brand[]" value="red" class="brand"> Red<br />
    <input type="radio" name="brand[]" value="green" class="brand"> Green<br />

$('.brand').rules('add',{
  required : true,
});
Rahul
  • 2,374
  • 2
  • 9
  • 17