Here is my function:
function checkRadio (frmName, rbGroupName) {
var radios = document[frmName].elements[rbGroupName];
for (var i=0; i <radios.length; i++) {
if (radios[i].checked) {
return true;
}
}
return false;
}
Here is my function call:
if (!checkRadio("purchaseOrder","credit_card"))
{
alert("You must select a payment type");
return false;
}
I'm trying to make sure there is a radio button selected when an HTML form is submitted. My code says there is nothing selected, even when one of the radio buttons have been chosen. I have a pretty long form and a lot of validation, so I am just going to post the code specific to the radio button. If you would like to see more, let me know.
Radio buttons:
Payment Method: <input type="radio" name="credit_card" value="visa" /> Visa <input type="radio" name="credit_card" value="master" /> MasterCard <input type="radio" name="credit_card" value="amex"/> American Express<br />
Function to validate:
function radioVal(btn) {
var cnt = -1;
for (var i=btn.length-1; i > -1; i--) {
if (btn[i].checked) {cnt = i; i = -1;}
}
if (cnt > -1) return btn[cnt].value;
else return null;
}
Call to function (within validate function for submit button)
//validate payment method
var btn = radioVal(document.forms["purchaseOrder"]["card_num"]);
if (btn == null)
{
alert("You must select a payment type");
return false;
}
I have also tried this inside the validation function:
radio = document.forms["purchaseOrder"]["credit_card"];
for (var i = 0; i< 3; i++)
{
if (radio[i].value ==null)
{
alert("You must choose a payment type");
return false;
}
}
Thanks for any direction you can give, I think I am nit understanding how radio validation works in general.
This post has been edited by synlight: 30 July 2012 - 10:04 AM

New Topic/Question
Reply



MultiQuote


|