Drop Down List Validation In ASP.NET Using JavaScript

Hi all,
 
Today, we are going to learn how to validate the Drodown list usign Java Script, in ASP.NET.
 
Create the Dropdown List and a button, as shown in the below code.
 
ASP.NET Code 
  1. <body>  
  2.     <form id="form1" runat="server">  
  3.     <div>  
  4.       
  5.         <asp:DropDownList ID="ddlNumber" runat="server">  
  6.             <asp:ListItem>--Select--</asp:ListItem>  
  7.             <asp:ListItem>1</asp:ListItem>  
  8.             <asp:ListItem>2</asp:ListItem>  
  9.             <asp:ListItem>3</asp:ListItem>  
  10.             <asp:ListItem>4</asp:ListItem>  
  11.         </asp:DropDownList>  
  12.         <br />  
  13.         <asp:Button ID="btnClick" runat="server" Text="Click" OnClientClick="return userValid()"/>  
  14.       
  15.     </div>  
  16.     </form>  
  17. </body>  
Now, write a JavaScript code to validate the dropdown list.

Note - Write the JavaScript code in "Head" Tag.
 
JavaScript Code 
  1. <script type="text/javascript">  
  2.        function userValid() {  
  3.            var e = document.getElementById("ddlNumber");  
  4.            var strUser1 = e.options[e.selectedIndex].text;  
  5.            if (strUser1 == "--Select--"//for text use if(strUser1=="Select")  
  6.            {  
  7.                alert("Please select a Number");  
  8.                return false;  
  9.            }  
  10.        }   
  11.      
  12.    </script>