Introduction
Here we have to discuss how to validate a Dropdownlist using javascript.we can also validate a dropdownlist using RequireFieldValidator.
Language
Java script
Implementation
Validator using Client Side :- JavaScript {codecitation class="brush: javascript; gutter: true;" width="500px"} function CountryCheck(source, args) { var ddlCountry = document.getElementById(””); var country =ddlCountry.options[ddlCountry.selectedIndex].value; if (country == “0″) { args.IsValid = false; } else { args.IsValid = true; } }
{/codecitation}
Html Code
{codecitation class="brush: html; gutter: true;" width="500px"} <td> <asp:DropDownList ID="DropDownList1" runat="server"> <asp:ListItem Text="Select Country" Selected="True"></asp:ListItem> <asp:ListItem Text="Japan"></asp:ListItem> <asp:ListItem Text="USA"></asp:ListItem> <asp:ListItem Text="Egypt"></asp:ListItem> <asp:ListItem Text="UK"></asp:ListItem> </asp:DropDownList> <asp:CustomValidator ID="CustomValidator1" ControlToValidate="DropDownList1" runat="server" ErrorMessage="Please Select Country" ClientValidationFunction=”CountryCheck”></asp:CustomValidator> </td> {/codecitation}
Conclusion For this we use CustomValidator so write a function to validate a Dropdownlist and pass the function to Clientvalidationfunction property of Customvalidator. Thank you Venkat
|