Total members 11895 |It is currently Sun Dec 22, 2024 2:04 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





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>
<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>




Author:
Newbie
User avatar Posts: 23
Have thanks: 1 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : Javascript Validation On Multiple Select Lists
 Javascript select menu validation issue     -  
 avoid multiple submit clicks using javascript     -  
 validation using JavaScript     -  
 Email validation in Javascript     -  
 lists merge     -  
 select query example in php     -  
 Select odd rows from table     -  
 Change the content of select tag using JQuery     -  
 Select all links with a target attribute value NOT _blank     -  
 SELECT-Group by and Order by,Having Clause,count(),Joins     -  



cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com