user3835327
user3835327

Reputation: 1204

Jquery validation for radio box

May i know how can i do validation for radio button in jquery? I need user to select items from the radio list, if none of the items is select when they submit the form the system will shows the error message at label. Any helps would be appreciated.

Please check the jsfiddle for sample.

Jsfiddle :1 Demo

Upvotes: 0

Views: 54

Answers (3)

AntonyZ
AntonyZ

Reputation: 43

You can simple use $("[name='SUBCLS']").is(":checked") Like this:

$(document).ready(function () {
    $('#submitbutton').click(function (e) {
        var res = $("[name='SUBCLS']");
        if (!res.is(":checked")) {
            fnValidate();
        }
    });
});

Upvotes: 1

Balachandran
Balachandran

Reputation: 9637

use $("[name=SUBCLS]:checked").length

$('#submitbutton').click(function (e) {
    if (!$("[name=SUBCLS]:checked").length || $.trim($("#VEHNO").val()) == '') {
        e.preventDefault();
        $("#optionalerrMsg").show();
    }else{
         $("#optionalerrMsg").hide();
    }

});

UPDATED DEMO

Upvotes: 1

Guruprasad J Rao
Guruprasad J Rao

Reputation: 29683

DEMO

ids must be always unique in the DOM so input type="radio" cannot have duplicate ids

Instead I have added a class to your radio button and below will be the reflection of the same:

<input class="rds" type="radio" name="SUBCLS" id="SUBCLS" value="CO" id="CheckboxGroup1_0" style="vertical-align: -2px;" />Comprehensive
<input class="rds" type="radio" name="SUBCLS" value="TF" id="CheckboxGroup1_1" />Third Party Fire &amp; Theft
<input class="rds" type="radio" name="SUBCLS" value="TP" id="CheckboxGroup1_2" />Third Party
<div class="errmsg"></div> <!--A div to display error message-->

in your fnValidate()

if(!$(".rds:checked").length)
{
      $(".errmsg").text('Please select a value from radio')
}

Upvotes: 1

Related Questions