5
votes

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?

3

3 Answers

6
votes

you can use javascript ValidatorEnable function

ValidatorEnable(document.getElementById('<%= ValidatorID.ClientID %>'), true);
1
votes

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

0
votes

The whole point of the validators is that they are to be used for both server side and client side validation.

If you only want to validate on the server side, don't use a validator and simply check that the values are valid and report an error.