Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery validation custom error message display for radio button

I'm using jQuery validation and trying to get the error message for a pair of radio buttons to show up before the first button.

Here is the validation:

errorPlacement: function(error, element) {
if (element.attr("type") == "radio")
   error.insertBefore(element.first());
 else
   error.insertAfter(element);
}

Here is the html:

<div class="full">
<label id="sample">Have you received your sample pack?</label>

<input type="radio" name="sample" id="sample_yes" value="yes" />
<label for="eliteflexsample_yes">Yes</label>

<input type="radio" name="sample" id="sample_no" value="no" />
<label for="eliteflexsample_no">No</label>    
</div>

Right now the error is showing up after the first radio button.

like image 706
Miles Pfefferle Avatar asked Jun 20 '12 15:06

Miles Pfefferle


People also ask

How to put validation on radio button?

Approach 1: First wrap Radio buttons and its label by using form-check-inline class. Then add img tag within the above wrap after label tag. Use default required validation by adding required attribute of radio button.

How to check validation for radio button in JavaScript?

Using Input Radio checked property: The Input Radio checked property is used to return the checked status of an Input Radio Button. Use document. getElementById('id'). checked method to check whether the element with selected id is check or not.

Which validation is used for radio button?

Setup Radio Button Validation It will return the value of the button within the group that is selected, or return a null value if no button in the group is selected. For example, here is the code that will perform the radio button validation: var btn = valButton(form. group1);


1 Answers

This is working:

$(document).ready(function() {

    $('form').validate({

        // your validation options,

        errorPlacement: function(error, element) {
            if (element.attr("type") == "radio") {
                error.insertBefore(element);
            } else {
                error.insertAfter(element);
            }
        }

    });

});​

Working Demo:

http://jsfiddle.net/DR5Aw/2/

like image 131
Sparky Avatar answered Sep 28 '22 04:09

Sparky