This example is showing how to validate two drop down list using a single javascript function for required
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('<%=ddlCountry.ClientID %>');
else
ddl = document.getElementById('<%=ddlState.ClientID %>');
if (ddl != null) {
args.IsValid = !(ddl.options[ddl.selectedIndex].value == 0);
}
else
args.IsValid = true;
}
No comments:
Post a Comment
Please add your valuable comments about this post if it helped you. Thanks