PDA

View Full Version : Form Validating Multiple Radio Buttons



kraftomatic
Nov 25th, 2003, 02:32 PM
Hey All,

I want to validate that each radio button in a survey has been filled out. There are about 30 questions. I've seen this code, but wanted to ask if there was something easier/cleaner that could handle ALL the buttons:

function radioButton(field)
{
var len = field.length ;
var check = true;

for (var i = 0; i < len; i++)
{

if (field[i].checked == true)
{
check = true;
break;
}
if (field[i].checked == false)
{
check = false;
}
}
if (check)
{
return true;
}
else
{
return false;
}
}


Thanks.

Kor
Nov 25th, 2003, 03:27 PM
It looks ok to me...

This script can handle all the buttons, but it depends what the parameter field means...

kraftomatic
Nov 25th, 2003, 03:34 PM
That will just handle one radio button though. How would I set it up to loop through all the radio elements on a page?

adios
Nov 25th, 2003, 05:17 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>untitled</title>
<script type="text/javascript" language="javascript">

function checkRadios(oForm)
{
var el, i = 0, grp, rad, focus_me = null, sMsg = '';
var scrAdj = -24; //fine-tunes scrolling to first unset radio
while (el = oForm.elements[i++])
if (el.type == 'radio')
{
grp = oForm.elements[el.name]; j = 0;
while (rad = grp[j++])
if (rad.checked)
break;
if (j > grp.length)
{
sMsg += 'o > ' + ((el.alt) ? el.alt : el.name) + '\n';
if (focus_me == null)
focus_me = el;
}
i += grp.length - 1;
}
if (sMsg != '')
{
var line = '_____________________________';
sMsg = line + '\nThe following items were unselected:\n\n' + sMsg;
sMsg += line + '\nPlease complete and re-submit.\nThank you.\n\n';
alert(sMsg);
if (focus_me.focus)
{
focus_me.focus();
scrollBy(0, scrAdj);
}
return false;
}
return true;
}


</script>
</head>
<body>
<form action="javascript&#58;alert('ok')" onsubmit="return checkRadios(this)">
<strong>some things</strong><br />
<!-- set alt attribute to message string -->
<input id="things1" type="radio" name="things" value="1" alt="some things" /> thing 1<br />
<input id="things2" type="radio" name="things" value="2" /> thing 2<br />
<input id="things3" type="radio" name="things" value="3" /> thing 3<br /><br />
<strong>my stuff</strong><br />
<input id="stuff1" type="radio" name="stuff" value="nice" alt="my stuff" /> nice stuff<br />
<input id="stuff2" type="radio" name="stuff" value="nicer" /> nicer stuff<br />
<input id="stuff3" type="radio" name="stuff" value="fab" /> absolutely fab stuff<br /><br />
<strong>your choice</strong><br />
<input id="choice1" type="radio" name="choice" value="y" alt="your choice" /> yes<br />
<input id="choice2" type="radio" name="choice" value="n" /> no<br /><br />
<strong>da foo?</strong><br />
<input id="foo1" type="radio" name="foo" value="bar" alt="da foo?" /><br />
<input id="foo2" type="radio" name="foo" value="hah" /><br />
<input id="foo3" type="radio" name="foo" value="feh" /><br /><br />

<small>scroll down<br />scroll down
<br />scroll down<br />scroll down<br />scroll down<br />scroll down<br />scroll down<br />scroll down<br />scroll down<br />scroll down<br />scroll down<br />scroll down<br />scroll down<br />scroll down<br />scroll down<br />scroll down<br />scroll down
<br />scroll down<br />scroll down<br />scroll down<br />scroll down<br />scroll down<br />scroll down<br />scroll down<br />scroll down<br />scroll down<br />scroll down<br />scroll down<br />scroll down<br />scroll down<br />scroll down<br />scroll down
<br />scroll down<br />scroll down<br />scroll down<br />scroll down<br />scroll down<br />scroll down<br />scroll down<br />scroll down<br />scroll down<br />scroll down<br />scroll down<br />scroll down<br />scroll down<br />scroll down<br />scroll down
<br />scroll down<br />scroll down<br />scroll down<br />scroll down<br />scroll down<br />scroll down<br />scroll down<br />scroll down<br />scroll down<br />scroll down<br />scroll down<br />scroll down<br />scroll down<br />scroll down<br />scroll down</small><br /><br />

<input type="submit" value="done">
</form>
</body>
</html>

kraftomatic
Nov 25th, 2003, 07:49 PM
Thank you sir. This is what I was looking for.