Click to See Complete Forum and Search --> : multiple <select> verification


yaron
10-02-2003, 04:41 AM
Hello all,
I guess this is an easy one...
I have in my form a multiple select field by the name of names[] (its an array of course).
I need to add to my submit check function a check to verify that at least one option was selected.
How can I do that?

Thanks

Khalid Ali
10-02-2003, 05:53 AM
you reference the each select box and get the selected index property....

Charles
10-02-2003, 06:34 AM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<title>Example</title>

<script type="text/javascript">
<!--
function verify (f) {
var selected = false;
for (var i=0; i<f.names.length; i++) {if (f.names[i].selected) selected = true}
if (!selected) {alert ('Please select and option or two.'); f.names.focus(); return false}
}
// -->
</script>

<form action="" onsubmit="return verify(this)">
<div>
<select multiple name="names">
<option>Fee</option>
<option>Fie</option>
<option>Foe</option>
<option>Fum</option>
</select>
<button type="submit">Submit</button>
</div>
</form>