Here is an example of how to write a common client validation function while using similar kind of validations on more than one asp.net control.
This example is showing how to validate two drop down list using a single javascript function for required
ASP.NET Markup
Javascript client validation function:
ASP.NET Markup
- <asp:DropDownList ID="ddlCountry" runat="server" AutoPostBack="True"
- onselectedindexchanged="ddlCountry_SelectedIndexChanged" CausesValidation="false">
- asp:DropDownList>
- <asp:CustomValidator ID="cvCountry" runat="server" ControlToValidate="ddlCountry"
- EnableClientScript="true" ValidationGroup="REG" Display="None" ErrorMessage="Country is required" Text=" "
- ClientValidationFunction="validateCbo">asp:CustomValidator>
- <asp:DropDownList ID="ddlState" runat="server">
- asp:DropDownList>
- <asp:CustomValidator ID="cvState" runat="server" ControlToValidate="ddlState"
- EnableClientScript="true" ValidationGroup="REG" Display="None" ErrorMessage="State is required" Text=" "
- ClientValidationFunction="validateCbo">asp:CustomValidator>
Javascript client validation function:
- function validateCbo(source, args) {
- var ddl = null;
- if(source.controltovalidate.indexOf('Country')>-1)
- ddl = document.getElementById('');
- else
- ddl = document.getElementById('');
- if (ddl != null) {
- args.IsValid = !(ddl.options[ddl.selectedIndex].value == 0);
- }
- else
- args.IsValid = true;
- }
No comments:
Post a Comment