Disable validation based on option
In a webpage, there are multiple validators and a dropdown. In the dropdown, if the option selected is Y, all validators should fire. If option selected is N, then all validators should be disabled. The server-side code is simple. How can this be done in javascript?
The HTML of the page looks like this:
<asp:DropDownList ID="ddlList" runat="server" OnChange="ValidateIf();"> <asp:ListItem Value="Y" Text="Yes"></asp:ListItem> <asp:ListItem Value="N" Text="No"></asp:ListItem> </asp:DropDownList> <asp:TextBox ID="txtName" runat="server"></asp:TextBox> <asp:RequiredFieldValidator ID="val" runat="server" ControlToValidate="txtName" ValidationGroup="Group1" Display="Dynamic" ErrorMessage="Please enter name" > </asp:RequiredFieldValidator> <asp:Button ID="btn" runat="server" Text="Submit" ValidationGroup="Group1" CausesValidation="true" />
When the dropdown is changed, then the javascript function - ValidateIf() is fired. This contains logic to enable / disable the validators. The function ValidatorEnable() is used for each validator
function ValidateIf() { var list = document.getElementById('ddlList'); var val = document.getElementById('val'); if (list.options[list.selectedIndex].value == "Y") { ValidatorEnable(val, true); } else { ValidatorEnable(val, false); } }
Posted by Vijay on 28-Aug-2010 01:28 PM
Category : Javascript
Category : Javascript
Commented by
ray
at 01-Oct-2010 07:33 AM
Javascript post #1
Commented by
Shailendra at 29-Oct-2010 01:28 PM
What is special about this post?


