jquery - Validate a form to ensure atleast one radio button is checked in a group -
i making form radio buttons in it. able validate other input except radio button. got accepted solution in site
using jquery check if no radio button in group has been checked
but not working me
http://jsfiddle.net/ghan_123/trr185l2/2/
my code
<input type='radio' name='datetime' value='1'>1 <input type='radio' name='datetime' value='2'>2 <input type='radio' name='datetime' value='3'>3 <input type='radio' name='datetime' value='4'>4 <input type='radio' name='datetime' value='5'>5 <br><button id='buttonnew'>validate</button>
jquery
$(document).ready(function(){ $("#buttonnew").click(function(){ var selection=document.queryselector('input[name="datetime"]:checked').value; // method if(selection=='') { alert('please select value'); } else { alert('your selected value '+selection); } // end method // other method ( given in site link above) if (!$("input[name='datetime']:checked").val()) { alert('nothing checked!'); } else { alert('one of radio buttons checked!'); } // end other method }); });
when nothing checked no alert message appears. else both give alert message on select 1 of these.
the problem here trying value
of radio button when none checked
, used error on console
. first need check whether checked
, if yes assign value else assign empty value below:
$(document).ready(function(){ $("#buttonnew").click(function(){ var selection=document.queryselector('input[name="datetime"]:checked')!=null? document.queryselector('input[name="datetime"]:checked').value:""; //conditional operator if(selection!='') { alert('your selected value '+selection); } else { alert('please select time slot'); } }); });
Comments
Post a Comment