...

View Full Version : Easiest way to go about validating a multi-named radios



jcdevelopment
09-09-2008, 08:59 PM
I have a large form for a user to fill out. There are probably about 50 different named sections of radio buttons. What would be the easiest way of validating them.

EX of what i have (that doesnt work)



function checkRadio (overview) {
var radios = document[overview];
for (var i=0; i <radios.length; i++) {
if (radios[i].checked) {
return true;
}
}
return false;
alert("bad");
}


html


<form id="overview" name="overview" action="" onsubmit="checkRadio(this)">

<input name="lawn" type="radio" value="g" />
<input name="lawn" type="radio" value="g" />
<input name="lawn" type="radio" value="g" />

<input name="house" type="radio" value="g" />
<input name="house" type="radio" value="g" />
<input name="house" type="radio" value="g" />


and about 48 more of those. Anyone have an easy solution to this, or any help.

Thanks

Arty Effem
09-10-2008, 01:17 AM
I have a large form for a user to fill out. There are probably about 50 different named sections of radio buttons. What would be the easiest way of validating them.

In the specified form, this function should alert the names of any unchecked radio or checkboxes (groups or singles)

Usage: <form ........ onsubmit="return allChecked(this)">


<script type='text/javascript'>

function allChecked(theForm)
{
var setNames=[], errorNames=[], elems=theForm.elements, len=elems.length, group, ok=false;

for(var i=0; i<len; i++)
if(elems[i].type && elems[i].type.match(/radio|checkbox/) && !setNames[ elems[i].name ] )
{
setNames[ elems[i].name ] = true;

group=elems[elems[i].name];

ok=false;

if(group.length)
for(var j=0, bLen = group.length; j < bLen && !(ok=group[j].checked); j++)
;
else
ok=group.checked;

if(!ok)
errorNames.push(elems[i].name);
}

if( !(ok=(errorNames.length==0)) )
alert('Please check one in each of these groups:\n\n'+errorNames);

return ok;
}

</script>

jcdevelopment
09-10-2008, 03:06 PM
Thanks, it worked great. I understand a good deal of why its working and what its doing.. but for some reason it is still submitting the form. Do you know anyway to stop that. I tried returning it false but it still went.

Thanks

EDIT** Nevermind, i missed the return on the submit


onsubmit="return function"



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum