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
- <body>
- <form id="form1" runat="server">
- <div>
-
- <asp:DropDownList ID="ddlNumber" runat="server">
- <asp:ListItem>--Select--</asp:ListItem>
- <asp:ListItem>1</asp:ListItem>
- <asp:ListItem>2</asp:ListItem>
- <asp:ListItem>3</asp:ListItem>
- <asp:ListItem>4</asp:ListItem>
- </asp:DropDownList>
- <br />
- <asp:Button ID="btnClick" runat="server" Text="Click" OnClientClick="return userValid()"/>
-
- </div>
- </form>
- </body>
Now, write a JavaScript code to validate the dropdown list.
Note - Write the JavaScript code in "Head" Tag.
JavaScript Code
- <script type="text/javascript">
- function userValid() {
- var e = document.getElementById("ddlNumber");
- var strUser1 = e.options[e.selectedIndex].text;
- if (strUser1 == "--Select--")
- {
- alert("Please select a Number");
- return false;
- }
- }
-
- </script>