Click here to Skip to main content
15,881,754 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to disablt the required field validator on the Disabled controls.

My situation is i have disabled 1 textbox , but when i click on the button , the required field validator is firing on the disabled textBox. Pls guide some solutions.
Posted

Disable the validator too when the field is disabled.
C#
RequiredFieldValidatorTB.Enabled = MyTextBox.Enabled
 
Share this answer
 
XML
When using validation controls in your ASP.NET pages you might want to disable validation in certain situations. The most common example is when you want to disable validation for a Cancel button. You can instruct the ASP.NET server control to disable just the client-side validation, or both the client-side and the server-side validation.


Disabling Client-Side Validation

If you want to perform only server-side validation and to avoid validation on the client, you can specify for certain ASP.NET Server controls not to not run client-side script validation. To disable client-side validation, set the validation control's EnableClientScript property to false.

<asp:Button id="CancelButton" runat="server" Text="Cancel" EnableClientScript="False" />


Disabling both Client-Side and Server-Side Validation

You can specify that individual controls on a Web Forms page cause a postback without triggering a validation check.

If you want to bypass validation for a specific ASP.NET Server control, you’ll have to set the control's CausesValidation property to false. Consider the ASP.NET code example below, showing how to disable validation for a Cancel button:

<asp:Button id="CancelButton" runat="server" Text="Cancel" CausesValidation="False" />

There is another way to disable a validation control, and you can accomplish it by setting the Enabled ASP.NET validation control property to false. Note that if you set Enabled to false, the ASP.NET validation control will not be rendered to the ASP.NET page at all:

<asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server" ControlToValidate="YourControlToValidate" ErrorMessage="Your error message here" Enabled="False" />
 
Share this answer
 
Comments
The Doer 3-Aug-12 6:14am    
thanks Fotrosi :)
Shwrv 12-May-16 4:05am    
Hi, how can i disable Rerquired field validator for only one fixed button for a textbox.
Hi Teju,
Try this in your page load:
C#
if(!textbox1.Enabled)
{
    rfvAdd1Insured.Enable = false;
    rfvAdd1Insured.ValidationGroup="";
    rfvAdd1Insured.ControlToValidate = null;
}


or

Enable or Disable ASP.Net Validation on client side
rfvOther: is a Required Field Validator
C#
//Syntax: 
ValidatorEnable(ValidatorContronName,Boolean); 
//Explanation:ValidatorContronName - This is ClientID of the Validation control. Boolean - true(Enable) / false(Disable) 
//Example: ValidatorEnable(document.getElementById('<%=rfvOther.ClientID%>'), false); 

You can implement this function according you logic.

It'll solve your problem.

--Amit
 
Share this answer
 
v3
Comments
The Doer 24-Jul-12 7:31am    
Thanks Amit Bhai.. :)
_Amy 24-Jul-12 7:32am    
Ha ha ha.. Still remember.. :) All the best Teju..
The Doer 24-Jul-12 7:35am    
arre bhai ruko yeh nahi aa raha saalaa....
_Amy 24-Jul-12 7:37am    
See my updated answer.
_Amy 24-Jul-12 7:46am    
Enable or Disable ASP.Net Validation on client side
rfvOther: is a Required Field Validator

//Syntax:
ValidatorEnable(ValidatorContronName,Boolean);
//Explanation:ValidatorContronName - This is ClientID of the Validation control.
Boolean - true(Enable) / false(Disable)
//Example:
ValidatorEnable(document.getElementById('<%=rfvOther.ClientID%>'), false);


You can implement this function according you logic.
Hi,

What about setting ControlToValidate property to null / none?

Please check these below links...

http://www.w3schools.com/aspnet/control_reqfieldvalidator.asp[^]

http://asp.net-tutorials.com/validation/required-field-validator/[^]

Regards
Sebastian
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900