In order to enable/disable server-side RequiredFieldValidator or validation of the page you need to call "ValidatorEnable" function in Javascript. The RequiredFieldValidator control belongs to System.Web.UI.WebControls
Syntax
public override void Validate()
{
bool enableValidators = !chkAllow.Checked;
rfvName.Enabled = enableValidators;
rfvPassword.Enabled = enableValidators;
base.Validate();
}
Property | Description |
---|---|
ControlToValidate | Specifies control name which needs to be validate. |
Display | Specifies behavior of the error message. |
EnableClientScript | Specifies whether or not client-side validation is enabled. |
Enabled | Specifies whether or not validation control is enabled or not. |
ErrorMessage | Specifies error messageto be display in validation summary. |
ForeColor | Specifies error message color. |
IsValid | Indicate if associate control passes validation or not. |
SetFocusOnError | Set focus on associate control if validation fails. |
Text | Specifies text to be display if validation fails. |
ValidationGroup | Specifies validation group name. |
Validate | Update the isvalid propery. |
ValidateGetValidationProperty | Determineing validation property of a control, if it exists. |
InitialValue | Specifies initial value of input control. |
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="https://www.w3.org/1999/xhtml">
<head runat="server">
<title>ASP.NET Enable Disable RequiredFieldValidator from server-side Example</title>
</head>
<body>
<form id="Form" runat="server">
<div style="width: 619px">
<asp:Label ID="lblTitle" runat="server" Font-Bold="True" Font-Size="Medium"
Font-Underline="True" Text="ASP.NET Enable/Disable Required Field from server-side Example"></asp:Label>
<br /><br />
<table border="0" width="500px" cellpadding="0" cellspacing="5">
<tr><td>
<asp:Label ID="lblName" runat="server" Font-Bold="True" Font-Size="Small"
Text="User name:"></asp:Label>
</td><td>
<asp:TextBox ID="txtName" runat="server" Height="22px" Width="175px"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvName" runat="server" Enabled="false"
ControlToValidate="txtName" ErrorMessage="User name can not be empty"></asp:RequiredFieldValidator>
</td></tr>
<tr><td>
<asp:Label ID="lblPassword" runat="server" Font-Bold="True" Font-Size="Small"
Text="Password:"></asp:Label>
</td><td>
<asp:TextBox ID="txtPassword" runat="server" Height="22px" TextMode="Password"
Width="175px"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvPassword" runat="server"
ControlToValidate="txtPassword" ErrorMessage="Password can not be empty"
Enabled="false"></asp:RequiredFieldValidator>
</td></tr>
<tr><td colspan="2">
<asp:CheckBox ID="chkAllow" runat="server" Text="Allow me without validation"
AutoPostBack="true" oncheckedchanged="chkAllow_CheckedChanged"
CausesValidation="false" />
</td></tr>
<tr><td colspan="2">
<asp:Button ID="btnDisabledValidation" runat="server" Font-Bold="True" Text="Validate"
Font-Size="Small" onclick="btnDisabledValidation_Click" CausesValidation="false" />
<asp:Label ID="lblMessage" runat="server" Font-Bold="True" Font-Size="Small" style="color:Red;"
Text=""></asp:Label>
</td></tr>
</table>
</div>
</form>
</body>
</html>
using System;
using System.Web.UI;
public partial class _Default : System.Web.UI.Page
{
public override void Validate()
{
bool enableValidators = !chkAllow.Checked;
rfvName.Enabled = enableValidators;
rfvPassword.Enabled = enableValidators;
base.Validate();
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnDisabledValidation_Click(object sender, EventArgs e)
{
Page.Validate();
if (Page.IsValid)
{
lblMessage.Text = "It's valid on server side";
}
else
{
lblMessage.Text = "It's not valid on server side";
}
}
protected void chkAllow_CheckedChanged(object sender, EventArgs e)
{
rfvName.Text = "";
rfvPassword.Text = "";
}
}
Above example will produce following output