Connecting Tech Pros Worldwide Help | Site Map

List Box Validation

Member
 
Join Date: Dec 2007
Posts: 44
#1: Oct 15 '09
hi,

Following function i am using for validating list box. In list box user can select only 5 item .

Expand|Select|Wrap|Line Numbers
  1.  function Validate()
  2.     {
  3.           var lblCount=0;
  4.           var lbGenre = document.getElementById('<% Response.Write(lstbox.UniqueID); %>');
  5.            for(var x = 0; x < lbGenre.options.length; x++)
  6.             {
  7.                  if(lbGenre.options[x].selected)
  8.                  {
  9.                     lblCount+=1;
  10.                  }
  11.             }
  12.             if(lblCount > 5)
  13.             {              
  14.               alert("maximum five!");  
  15.               return false;
  16.             }
  17.     }

That is working fine. i want to how to unselect the six item selected from list box?


regards
veena
Member
 
Join Date: Dec 2007
Posts: 44
#2: Oct 16 '09

re: List Box Validation


Can any one please tell me How to unselect one item from list box?

Regards
Veena
RamananKalirajan's Avatar
Needs Regular Fix
 
Join Date: Mar 2008
Location: Chennai - India
Posts: 348
#3: Oct 16 '09

re: List Box Validation


Hi Veena,
Its difficult to unselect a particular option in multiselect.

Thanks and Regards
Ramanan Kalirajan
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#4: Oct 16 '09

re: List Box Validation


Quote:

Originally Posted by RamananKalirajan View Post

Its difficult to unselect a particular option in multiselect.

doesn’t the usual ctrl+click work? (untested)
RamananKalirajan's Avatar
Needs Regular Fix
 
Join Date: Mar 2008
Location: Chennai - India
Posts: 348
#5: Oct 16 '09

re: List Box Validation


She is asking about unselcting through JS.. It can be done using document.getElementById('sampleSelect').options[selectedIndex].selected="false", this will work in Mozilla but not in IE. This is a problem.

Thanks and Regards
Ramanan Kalirajan
ivosilva's Avatar
Newbie
 
Join Date: Oct 2006
Posts: 17
#6: 4 Weeks Ago

re: List Box Validation


Hello, veenna!

I have tested the following code in IE8, Firefox 3.5 and Chrome 3.0 and it seems to work fine (I have no previous versions of IE installed to test it).

Expand|Select|Wrap|Line Numbers
  1. function validate() {
  2.             var max = 2;
  3.             var lblCount = 0;
  4.             var lbGenre = document.getElementById('<% Response.Write(lstbox.UniqueID); %>');
  5.  
  6.             for(var x = 0; x < lbGenre.options.length; x++) {
  7.  
  8.                 if(lbGenre.options[x].selected) {
  9.  
  10.                     lblCount++;
  11.  
  12.                     if(lblCount > max) {
  13.                         lbGenre.options[x].selected = false;
  14.                     }
  15.                 }
  16.             }
  17.  
  18.             if(lblCount > max) {
  19.                 alert("You can only select a maximum of " + max + " options!");
  20.                 return false;
  21.             }
  22.         }
Best regards
RamananKalirajan's Avatar
Needs Regular Fix
 
Join Date: Mar 2008
Location: Chennai - India
Posts: 348
#7: 4 Weeks Ago

re: List Box Validation


In IE6, it wont be working yaar...

Thanks and Regards
Ramanan Kalirajan
ivosilva's Avatar
Newbie
 
Join Date: Oct 2006
Posts: 17
#8: 4 Weeks Ago

re: List Box Validation


Hello, RamananKalirajan!

I've just tested it under IE6 and it works ok.

Have you tested my code using IE6?

Best regards!
Reply