In this article I will explain how to enable a RequiredFieldValidator when CheckBox is checked and disable a RequiredFieldValidator when CheckBox is unchecked in ASP.Net using JavaScript and jQuery.
In order to enable or disable ASP.Net RequiredFieldValidator, the ASP.Net ValidatorEnable client side JavaScript function is used.
You might also like to read:
Enable RequiredFieldValidator when CheckBox is checked in ASP.Net using JavaScript
The HTML Markup consists of an ASP.Net TextBox, a CheckBox to enable or disable the validation when it is checked or unchecked respectively and a Button to trigger the RequiredField Validator for the TextBox.
UserName:
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="valName" ControlToValidate="txtName" runat="server"
ErrorMessage="*Required" ForeColor="Red" ValidationGroup="Group1" />
<br />
Enable Validation:
<input type="checkbox" id="CheckBox1" onclick="ToggleValidator(this);" checked="checked" />
<br />
<asp:Button Text="Submit" runat="server" ValidationGroup="Group1" />
<script type="text/javascript">
function ToggleValidator(chk) {
var valName = document.getElementById("<%=valName.ClientID%>");
ValidatorEnable(valName, chk.checked);
}
</script>
The RequiredField Validator for the TextBox can be enabled or disabled by checking or unchecking the CheckBox respectively.
When the CheckBox is clicked the ToggleValidator JavaScript function is executed. Inside this function we first check whether the CheckBox is checked or unchecked and based on its state we either enable or disable the RequiredField Validator valName using the ValidatorEnable JavaScript function.
Enable RequiredFieldValidator when CheckBox is checked in ASP.Net using jQuery
The HTML Markup consists of an ASP.Net TextBox, a CheckBox to enable or disable the validation when it is checked or unchecked respectively and a Button to trigger the RequiredField Validator for the TextBox.
Email:
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="valEmail" ControlToValidate="txtEmail"
runat="server" ErrorMessage="*Required" ForeColor="Red" ValidationGroup="Group2" />
<br />
Enable Validation:
<input type="checkbox" id="CheckBox2 checked="checked" />
<br />
<asp:Button Text="Submit" runat="server" ValidationGroup="Group2" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(document).on("click", "#CheckBox2", function () {
var valEmail = $("[id*=valEmail]");
ValidatorEnable(valEmail[0], $(this).is(":checked"));
});
</script>
The RequiredField Validator for the TextBox can be enabled or disabled by checking or unchecking the CheckBox respectively.
A jQuery Click event handler has been assigned to the CheckBox, thus when the CheckBox is clicked, we first check whether the CheckBox is checked or unchecked and based on its state we either enable or disable the RequiredField Validator valEmail using the ValidatorEnable JavaScript function.
Demo
Downloads