By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,623 Members | 2,030 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,623 IT Pros & Developers. It's quick & easy.

List Box Validation

P: 65
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
Oct 15 '09 #1

✓ answered by ivosilva

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

Share this Question
Share on Google+
7 Replies


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

Regards
Veena
Oct 16 '09 #2

RamananKalirajan
100+
P: 607
Hi Veena,
Its difficult to unselect a particular option in multiselect.

Thanks and Regards
Ramanan Kalirajan
Oct 16 '09 #3

Dormilich
Expert Mod 5K+
P: 8,639
@RamananKalirajan
doesn’t the usual ctrl+click work? (untested)
Oct 16 '09 #4

RamananKalirajan
100+
P: 607
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
Oct 16 '09 #5

ivosilva
P: 17
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
Oct 18 '09 #6

RamananKalirajan
100+
P: 607
In IE6, it wont be working yaar...

Thanks and Regards
Ramanan Kalirajan
Oct 19 '09 #7

ivosilva
P: 17
Hello, RamananKalirajan!

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

Have you tested my code using IE6?

Best regards!
Oct 19 '09 #8

Post your reply

Sign in to post your reply or Sign up for a free account.