...

View Full Version : How to validate a multiple selection select box



markhartnady
05-31-2004, 04:19 PM
Okay.... I got this JavaScript:


function submitform()
{
switch (document.sessions.options.value)
{
case "total_sess":
if (document.pmap_list.options.value == "")
{
alert('Please select a P-map');
return false;
}
break;
case "total_country": break;
case "total_reseller": break;
case "total_subs": break;
default:
}

document.webstats.submit();
}
Validating this control:


<select name="pmap_list[]" class="dfont10" size=5 multiple>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>

Depending on what is selected here:


<td colspan="2">
<input type="radio" name="sessions" value="total_sess" {sess_sel}>
Total Number of Sessions<br>
<input type="radio" name="sessions" value="total_country" {country_sel}>
Sessions By Country<br>
<input type="radio" name="sessions" value="total_reseller" {reseller_sel}>
Sessions By Resellers<br>
<input type="radio" name="sessions" value="total_subs" {subs_sel}>
Session By Independant Subscribers<br>
<input type="radio" name="sessions" value="top10" {top10_sel}>
Top 10
</td>

Basically, I just wanna ensure that a "pmap_list" value is selected. The name must remain pmap_list[] as I am passing this as an array to the php page.

fredmv
06-07-2004, 04:31 AM
Use array notation instead:
if(document.forms[0]['pmap_list[]'].options.value == "")Assuming the element is contained within the first form in the documnt.

glenngv
06-07-2004, 06:23 AM
It should be:

if(document.forms[0]['pmap_list[]'].selectedIndex == -1)

But the checking for selected radio button is also wrong.

Should be:


function submitform()
{
var f = document.webstats;
for (var i=0;i<f.sessions.length;i++)
{
if (f.sessions[i].checked)
{
switch (f.sessions[i].value)
{
case "total_sess":
if (f.elements['pmap_list[]'].selectedIndex == -1)
{
alert('Please select a P-map');
return false;
}
break;
case "total_country": break;
case "total_reseller": break;
case "total_subs": break;
default:
}
break; //break from for-loop
}
}
f.submit();
}

fredmv
06-07-2004, 06:33 AM
Oops; very good call Glenn. I was more so looking at the syntax as opposed to the logic behind it; huge mistake on my part. Thanks for the correction.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum