Web Hosting Talk







View Full Version : Validation Form - Radio button group


mlopezco
02-11-2004, 12:07 PM
Hi. I am a newbie here, so please be nice :)

I can't figure out why my function isn't working. I am listing the code below, and if anyone can help or has other suggestions, please let me know! - Thanks!!!!!

<head>
<script language="JavaScript" type="text/JavaScript">

function validRadio(formField[i], fieldLabel, num) {
var result = true;

for (i=0, i<num, i++) {

if (!(formField[i].checked)) {
alert('Please select an answer for "' + fieldLabel + '".');
formField.focus();
result = false;
}
}

return result;
}

kckclass
02-14-2004, 02:34 AM
First, I don't know if you can focus on a radio... since this is ambiguous...

document.form1.radioselects.focus()

where you have a
<input type=radio name="radioselects" value="1">One

<input type=radio name="radioselects" value="2>Two

etc... WHICH element is going to get the focus? I don't think you can do that.


Second...I do have a 'which button is checked script around here and am digging... hang on a sec....


Ok, found it... haven't used this in awhile...


function whichButt(buttGroup)
{ for (var i = 0; i < buttGroup.length; i++)
{ if (buttGroup[i].checked)
{ return i+1
}
}
return 0
}

function ckMemb()
{ var mom1 = whichButt(form1.mkmem)
var mom2 = whichButt(form1.ffmem)
var mom3 = whichButt(form1.gbmem)
var mom4 = whichButt(form1.rbmem)
if ((mom1 == 0) && (mom2 == 0) && (mom3 == 0) && (mom4 == 0))
{ alert('Please check a Membership Level before proceeding.")
return false
}
return true
}

mkmem, ffmem, gbmem etc. are each seperate radio button groups and I believe mom1 will contain a value 0 (none) or 1 to N of WHICH button was clicked in a group.

You'll probably freak at my { marks (start on new line to line up with matching brace) but it should work for you.