Home > ASP.Net, Javascript > Disable Validation based on Condition

Disable Validation based on Condition


Hi all,

try this example to Disable Validation based on Condition


<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" />

and use this javascript :


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);
 }
}

Hope this helps

Good Luck.

Advertisements
Categories: ASP.Net, Javascript
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: