I have the following code for validating Radio buttons:
When I select the radio button and submit, it gives me a message
'Please select a Section and press "Submit"'.
I donot have any problem when there are more than one radio buttons to choose from.
I am not able to figure out what the problem is. Any help will be appreciated.
Thank you.
PKS
function checkSecRadioAlert() {
var el = document.forms[0].elements;
for(var i = 0 ; i < el.length ; ++i) {
if(el.type == "radio") {
var radiogroup = el[el.name]; // get the whole set of radio buttons.
var itemchecked = false;
for(var j = 0 ; j < radiogroup.length ; ++j) {
if(radiogroup[j].checked) {
itemchecked = true;
break;
}
}
if(!itemchecked) {
alert("Please select a Section and press \"\Submit\"\.");
if(el.focus)
el.focus();
return false;
}
}
}
return true;
}
<Form name='RearrSecForm' Method='POST' Action='RearrangeVarFrame.asp' onsubmit='return checkSecRadioAlert(this);'>
<table>
<tr>
<td><input type='radio' value='1872' name='SecName'></td>
<td>Endowment</td> <tr>
<tr>
<td></td>
<td><input type='submit' value='Submit' name='Cnt'></td>
</tr>
</table>
</Form>
When I select the radio button and submit, it gives me a message
'Please select a Section and press "Submit"'.
I donot have any problem when there are more than one radio buttons to choose from.
I am not able to figure out what the problem is. Any help will be appreciated.
Thank you.
PKS
function checkSecRadioAlert() {
var el = document.forms[0].elements;
for(var i = 0 ; i < el.length ; ++i) {
if(el.type == "radio") {
var radiogroup = el[el.name]; // get the whole set of radio buttons.
var itemchecked = false;
for(var j = 0 ; j < radiogroup.length ; ++j) {
if(radiogroup[j].checked) {
itemchecked = true;
break;
}
}
if(!itemchecked) {
alert("Please select a Section and press \"\Submit\"\.");
if(el.focus)
el.focus();
return false;
}
}
}
return true;
}
<Form name='RearrSecForm' Method='POST' Action='RearrangeVarFrame.asp' onsubmit='return checkSecRadioAlert(this);'>
<table>
<tr>
<td><input type='radio' value='1872' name='SecName'></td>
<td>Endowment</td> <tr>
<tr>
<td></td>
<td><input type='submit' value='Submit' name='Cnt'></td>
</tr>
</table>
</Form>