View Full Version : verify checkboxes are filled out prior to posting
fitchic77
09-14-2002, 10:49 PM
Not sure how to do this
My checkboxes are named as:
<input type="checkbox" name="LOCATION_ARRAY[]" value="SF">
<input type="checkbox" name="LOCATION_ARRAY[]" value="NF">
Do I just do a loop and verify at least one is checked?
Need more info.
Thank a WHOLE BUNCH!
Andrea
:D
joh6nn
09-14-2002, 11:33 PM
that sounds fine to me. pseudo code follows:
<script>
function testChecked() {
for ( var i = 0; i < checkboxes.length; i++ ) {
if ( checkboxex[i].checked ) {
return true;
}
}
return false;
}
</script>
adios
09-14-2002, 11:47 PM
Some real-o-code:
<html>
<head>
<title>Converter</title>
<script type="text/javascript" language="javascript">
//prompts
var msg = Object;
msg['LOCATION_ARRAY[]'] = 'Please check one of the Location boxes.';
function checkform(f) {
var el, e = 0, i, grp;
while (el = f.elements[e++]) {
if ((el.type == 'checkbox' || el.type == 'radio') && el.name && f[el.name].length) {
grp = f[el.name];
i = 0;
while (el = grp[i++]) if (el.checked) break;
if (i > grp.length) {
alert(msg[grp[0].name]);
grp[0].focus();
return false;
}
e += grp.length - 1;
}
}
return true;
}
</script>
</head>
<body>
<form action="javascript:alert('Submitted !')" method="post"
onsubmit="return checkform(this)">
<h4>Location</h4>
SF <input type="checkbox" name="LOCATION_ARRAY[]" value="SF" /><br />
NF <input type="checkbox" name="LOCATION_ARRAY[]" value="NF" /><br /><br />
<input type="submit" value="DONE" />
</form>
</body>
</html>
Easily extensible...
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.