Enable or Disable ASP.NET Validation on Client Side

Enable or Disable ASP.NET Validation on Client Side

We can enable or disable the validation controls of asp.net using the ValidatorEnable() method of java script. So based on some condition we can decide how validation controls will work. Suppose i have a one check box, if user selects this check box then a next text box is required otherwise it is optional.

So below is my Text box along with RequiredfieldValidator.

<asp:TextBox ID=”txtFirstName” runat=”server” MaxLength=”20″ CssClass=”txtboxNumeric”
Width=”100px”></asp:TextBox>

<asp:RequiredFieldValidator ID=”reqFirstName” runat=”server” CssClass=”errormsg”
Display=”Dynamic” ControlToValidate=”txtFirstName” ErrorMessage=”*” ValidationGroup=”valgrp”
Enabled=”false”></asp:RequiredFieldValidator>

to enable above RequiredfieldValidator we just need to write down below java script code in our function.

ValidatorEnable(document.getElementById($(‘#<%=reqFirstName.ClientID %>’).attr(‘id’)), true);

to disable above RequiredfieldValidator use below code.

ValidatorEnable(document.getElementById($(‘#<%=reqFirstName.ClientID %>’).attr(‘id’)), false);

 

Leave a Reply