I have TextBox with RequiredFieldValidator on my page. I also have link that calls some simple javascript.
<asp:TextBox ID="TextBox1" runat="server" />
<asp:RequiredFieldValidator
ID="RequiredFieldValidator4" runat="server" ControlToValidate="TextBox1"
Display="Dynamic" />
<asp:LinkButton ID="Link1" runat="server" OnClientClick="DoSomething(); return false;" CausesValidation="false"Text="Do" />
function DoSomething() {
textbox1.val("blah"); }
When user type something into textbox and then delete that text and focus next control - then validator fires. Then user can use link that adds text using javascript. TextBox1 text is no longer empty but RequiredFieldValidator still shows error message. How to prevent that?
Enable/Disable RequiredFieldValidator with Javascript. Use ValidatorEnable function from the Asp.net javacsript Script Library to Enable/Disable the validators on client side. Sometimes you may need to enable/disable validators on client side.
What it does is: Use a customValidator (not a RequiredFieldValidator) control that validates the textbox using a javascript function called Validator. The validator function checks if the checkbox is checked and validates the textbox.
you can use javascript ValidatorEnable
function
ValidatorEnable(document.getElementById('<%= ValidatorID.ClientID %>'), true);
I would recommend a CustomValidator
here since you do NOT want the normal RequiredFieldValidator
behavior. The CustomValidator
client method will only run on postback.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With