Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery Validate Group of Radio Buttons

I'm using jquery's validator plugin to validate my entire form. And I have a set of radio buttons like this:

<div class="radio_options" style="display:none; margin-left:7px;">
    <label><input type="radio" name="options" value="1"> Option1</label>
    <label><input type="radio" name="options" value="2"> Option2</label>
    <label><input type="radio" name="options" value="3"> Option3</label>
</div>

I know I can just put

class="required"

But it displays the required message next to option 1 making it look like that radio button is required. Rather I want to display a custom message above all the radio buttons requiring at least one be selected.

like image 855
Michael Benneton Avatar asked Nov 19 '25 06:11

Michael Benneton


1 Answers

$(".form").validate({
    ...
    ,errorPlacement: function (error, element) {
        if (element.is("input:radio")) {
            element.parents("div:first").before(error);
        } else {
            element.after(error);
        }
    }
    ...
});
like image 157
Halil Özgür Avatar answered Nov 21 '25 08:11

Halil Özgür



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!