Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validate Multiple Select Box 1

Status
Not open for further replies.

TanTrazz

Programmer
Aug 18, 2001
54
NL
Hi All,

Does someone know how to validate an multiple selectbox?

I want to max the selection to 4. When they select the fifth they have to get an message which tell them they can only select 4 options.

<form method="_GET" action="post.asp" name="form1">
<select name="ckb" size="20" multiple="multiple">
<option>Value1</option>
<option>Value2</option>
<option>Value3</option>
<option>Value4</option>
<option>Value5</option>
<option>Value6</option>
<option>Value7</option>
</select>
</form>

Thanks in advance.

TanTrazz
 
This code will not unselect any option after 4. To get that kind of functionality it's going to start to get messy. However, this will perform the check you're wanting:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<title>test</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<script type="text/javascript">

function howMany(selObj) {
   var totalChecked = 0;
   for (i = 0; i < selObj.options.length; i++) {
      if (selObj.options[i].selected) {
         totalChecked++;
      }
   }
   if (totalChecked > 4) {
      alert("You can't check more than 4 options");
   }
}

</script>
<style type="text/css"></style>
</head>
<body>

<select name="ckb" size="20" multiple="multiple" onchange="howMany(this)">
   <option>Value1</option>
   <option>Value2</option>
   <option>Value3</option>
   <option>Value4</option>
   <option>Value5</option>
   <option>Value6</option>
   <option>Value7</option>
</select>

</body>
</html>

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson
[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B> bites again.[/small]
 
Hi Kaht,

Thanks for the code it works fine!!

TanTrazz
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top