In my post, I discussed different client side APIs and Methods of asp.net validator Controls. Now suppose if you have customized validation on client side, what'll happen on server side? We need to do same on server side. Let's understand this step by step by taking same example. Suppose you have to disable password's requiredfieldvalidator when 'Allow me' checkbox is true. You have disabled validator on checkbox click using ValidatorEnable javascript method. Now on server side button click, if you validate and check for isValid, it is always false and you will see error message in validationsummary even if checkbox is checked. Client side bypasses validation but on server side it's stucked and our object is defeated.
So, you have to do SAME on server side before calling validate method. you need to disable password's requiredfield validator on checkbox true, then validate and check Page.IsValid on button click.
Alternatively, you can override Validate method and enable/disable validator in the method. see following code:
public override void Validate(string grp)
{
bool enableValidators = !CheckBoxAllowMe.Checked;
PasswordRequired.Enabled = enableValidators;
base.Validate();
}
protected void LoginButton_Click(object sender, EventArgs e)
{
Page.Validate("LoginUserValidationGroup");
if (Page.IsValid)
{
LabelMessage.Text = "It's valid on server side";
}
else
{
LabelMessage.Text = "It's NOT valid on server side";
}
}
When you set checkbox and click on button, requiredfieldvalidator is disabled on server side and Page.IsValid = true.
Server side Properties:
1. CausesValidation Property: For validation check(Mostly used to bypass validation on cancel button).
2. Enabled property of Validator Controls: To configure both side validation.
3. EnableClientScript property of Validator control: To configure client side validation only.
Disable All validators:
Suppose you have to disable all validators of a validation group.
Server Side:
//To enable/disable all validators of a validation group
foreach (BaseValidator val in Page.Validators)
{
if (val.ValidationGroup == "LoginUserValidationGroup")
{
val.Enabled = false;
}
}
Client Side:
for (i = 0; i < Page_Validators.length; i++) {
if (Page_Validators[i].validationGroup == "LoginUserValidationGroup") {
ValidatorEnable(Page_Validators[i], false);
}
}
Disable All Validators related to specific control:
Suppose you have to disable all validators for Password textbox.
Server Side:
//To enable/disable all validators of a validation group
foreach (BaseValidator val in Page.Validators)
{
if (val.ControlToValidate == "Password")
{
val.Enabled = false;
}
}
Client Side:
for (i = 0; i < Page_Validators.length; i++) {
if (Page_Validators[i].controltovalidate == "<%=Password.ClientID %>") {
ValidatorEnable(Page_Validators[i], false);
}
}
jQuery to Enable/Disable Validation:
In the article, I posted javascript method to enable/disable validator. You can do this using jQuery also. Here is the jQuery equivalent:
$("[id$='LoginjQButton']").click(function () {
if ($("[id$='CheckBoxAllowMe']").is(':checked')) {
ValidatorEnable($("[id$='PasswordRequired']")[0], false);
}
//ValidationSummaryOnSubmit("LoginUserValidationGroup");
if (Page_ClientValidate("LoginUserValidationGroup")) {
alert('it is valid');
return true;
}
else {
alert('No valid');
return false;
}
});
Hope, It helps. Share your opinion and tips on ASP.NET Validation.