Current location: Hot Scripts Forums » General Web Coding » JavaScript » Javascript Validation On Multiple Select Lists

Javascript Validation On Multiple Select Lists

 
Prev Previous Post   Next Post Next
  #1  
Old 07-08-09, 06:35 AM
gilbertsavier gilbertsavier is offline
New Member
 
Join Date: Jul 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Javascript Validation On Multiple Select Lists

Hi,
I just spent the better part of today working through a client side javascript validation challenge for our online VISIONS application. My objective was to alert a user when he or she makes a selection(s) (and/or non-selection) from a multiple drop-down select box where the choices are inputted into an array, as well as for a multiple list selection box where the choices are also placed into an array. I wanted to validate their selection to alert them if they forgot to select one of the values, or if they selected the "Other" choice from the select list but forgot to type the "Other" value in the provided text input field.

The validation was not as intuitive as I had hoped. Here's how I finally solved the first issue in the event a user forgets to select a value(s) from the multiple choice drop-down menu:
HTML Code:
 <html xmlns="http://www.w3.org/1999/xhtml">
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 <title>Untitled Document</title>
 <script>
 var minNum=1; var maxNum=3;
 function checkListBoxSize(){
 oSelect=document.getElementById("category_id");
 var count=0;
 for(var i=0;i<oSelect.options.length;i++){
       if(oSelect.options[i].selected)
             count++;
       if(count>maxNum){
             alert("Can't select more than 3");
             return false;
       }
 }
 if(count<1){
 alert("Must select at least one item");
 return false;
 }
 return true;
 }
 
 </script>
 </head>
 
 <body>
 <form onsubmit="return checkListBoxSize()">
 <select multiple="multiple" id="category_id" name="category_id" size="10" onchange="return false;">
   <option value="a">a</option>
   <option value="b">b</option>
   <option value="c">c</option>
   <option value="d">d</option>
   <option value="e">e</option>
 </select>
 <input name="" type="submit"/>
 </form>
 </body>
</html>
---------------------
Thanks & regards
Lokananth

Last edited by Nico; 07-08-09 at 07:32 AM.
Reply With Quote
 

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiple AJAX/PHP chained select? xenia_jazz PHP 3 01-19-09 01:57 AM
multiple select box with PHP zoliky PHP 4 11-02-06 04:26 AM
Multiple Select phppick JavaScript 3 04-11-05 01:09 AM
help with multiple select used with php and mysql isaacmlee PHP 2 10-15-04 02:34 PM
javascript multiple select menu for php? isaacmlee JavaScript 1 10-15-04 10:53 AM


All times are GMT -5. The time now is 01:36 AM.
vBulletin® Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.2 (Unregistered)