Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How disable RequiredFieldValidator in script

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?

like image 848
jlp Avatar asked Apr 22 '10 08:04

jlp


People also ask

How do I disable the RequiredFieldValidator?

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 is ValidatorEnable in Javascript?

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.


2 Answers

you can use javascript ValidatorEnable function

ValidatorEnable(document.getElementById('<%= ValidatorID.ClientID %>'), true);
like image 94
Muhammad Akhtar Avatar answered Oct 09 '22 08:10

Muhammad Akhtar


I would recommend a CustomValidator here since you do NOT want the normal RequiredFieldValidator behavior. The CustomValidator client method will only run on postback.

like image 43
Bryan Avatar answered Oct 09 '22 09:10

Bryan