Reputation: 1204
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
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
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();
}
});
Upvotes: 1
Reputation: 29683
id
s must be always unique in theDOM
soinput type="radio"
cannot have duplicateid
s
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 & 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