Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

radio button validation

Status
Not open for further replies.

nawrioj

Programmer
Feb 14, 2005
22
US
Hi
I'm trying to use this function to validate radio button but it doesn't seem to work. Can anybody tell me what's wrong with it ? What should I be putting on the radio button validation ?
This is the portion I add
else if (test.indexOf('radio') != -1)
{
if(!val)errors += '- '+args+' is required.\n';

this is the whole function

function MM_validateForm() { //v4.0
var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
for (i=0; i<(args.length-2); i+=3)
{
test=args[i+2]; val=MM_findObj(args);
if (val)
{
nm=val.name;
if ((val=val.value)!="")
{
if (test.indexOf('isEmail')!=-1)
{
p=val.indexOf('@');
if (p<1 || p==(val.length-1)) errors+='- '+nm+' must be a valid e-mail address.\n';
}
else if (test.indexOf('radio') != -1)
{
if(!val)errors += '- '+args+' is required.\n';
}
else if (test!='R')
{
num = parseFloat(val);
if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
if (test.indexOf('inRange') != -1)
{
p=test.indexOf(':');
min=test.substring(8,p); max=test.substring(p+1);
if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
}
}
}
else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n';
}
}
if (errors) alert('Please correct your information:\n'+errors);
document.MM_returnValue = (errors == '');
}

Thanks

nawrioj
 
Radio buttons do not have a selectedIndex like dropdowns do. As far as I know, the only way to validate them is to loop thru and check to see if any are checked. Try something like this:
Code:
<script language=javascript>
function validateRadio() {
   radioGroup = document.forms["blahForm"].elements["blahRadio"];
   validRadio = false;
   for (i = 0; i < radioGroup.length; i++) {
      validRadio = (validRadio || radioGroup[i].checked);
   }
   if (!validRadio) {alert("you must select a radio button");}
}
</script>
<body>
<form name=blahForm>
<input type=radio name=blahRadio>1<br>
<input type=radio name=blahRadio>2<br>
<input type=radio name=blahRadio>3<br>
<input type=radio name=blahRadio>4<br>
<input type=radio name=blahRadio>5<br>
<input type=radio name=blahRadio>6<br>
<input type=radio name=blahRadio>7<br>
<input type=button value='validate radio' onclick='validateRadio()'>
</form>
</body>

-kaht

Do the chickens have large talons?
 
I need to validate the other input in the form. Can I do <form method="post" action="contact_thankyou.php" onSubmit="validateRadio();MM_validateForm('address','','R','city','','R','state','','R','zip','','R','fullname','','R','homephone','','R','heardaboutus','','R','interestarea','','R','permanent_injury','','Rradio','life_changed','','Rradio','economic_damage','','Rradio');return document.MM_returnValue"> ????
 
why not just put the contents of validateRadio() inside of MM_validateForm? Take out the part where you were originally trying to check for the radio button validation and replace it with the code that I provided in the function above.

-kaht

Do the chickens have large talons?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top