List Box Validation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • veenna
    New Member
    • Dec 2007
    • 65

    List Box Validation

    hi,

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

    Code:
     function Validate()
        {
              var lblCount=0;
              var lbGenre = document.getElementById('<% Response.Write(lstbox.UniqueID); %>');
               for(var x = 0; x < lbGenre.options.length; x++)
                {
                     if(lbGenre.options[x].selected)
                     {
                        lblCount+=1;
                     }
                }
                if(lblCount > 5)
                {              
                  alert("maximum five!");  
                  return false;
                }
        }

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


    regards
    veena
  • veenna
    New Member
    • Dec 2007
    • 65

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

    Regards
    Veena

    Comment

    • RamananKalirajan
      Contributor
      • Mar 2008
      • 608

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

      Thanks and Regards
      Ramanan Kalirajan

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        Originally posted by RamananKaliraja n
        Its difficult to unselect a particular option in multiselect.
        doesn’t the usual ctrl+click work? (untested)

        Comment

        • RamananKalirajan
          Contributor
          • Mar 2008
          • 608

          #5
          She is asking about unselcting through JS.. It can be done using document.getEle mentById('sampl eSelect').optio ns[selectedIndex].selected="fals e", this will work in Mozilla but not in IE. This is a problem.

          Thanks and Regards
          Ramanan Kalirajan

          Comment

          • ivosilva
            New Member
            • Oct 2006
            • 17

            #6
            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).

            Code:
            function validate() {
            			var max = 2;
            			var lblCount = 0;
            			var lbGenre = document.getElementById('<% Response.Write(lstbox.UniqueID); %>');
            			
            			for(var x = 0; x < lbGenre.options.length; x++) {
            			
            				if(lbGenre.options[x].selected) {
            				
            					lblCount++;
            					
            					if(lblCount > max) {
            						lbGenre.options[x].selected = false;
            					}
            				}
            			}
            			
            			if(lblCount > max) {
            				alert("You can only select a maximum of " + max + " options!");
            				return false;
            			}
            		}
            Best regards

            Comment

            • RamananKalirajan
              Contributor
              • Mar 2008
              • 608

              #7
              In IE6, it wont be working yaar...

              Thanks and Regards
              Ramanan Kalirajan

              Comment

              • ivosilva
                New Member
                • Oct 2006
                • 17

                #8
                Hello, RamananKaliraja n!

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

                Have you tested my code using IE6?

                Best regards!

                Comment

                Working...