views:

156

answers:

3

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?

A: 

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.

Oded
I want validation to happen client-side. Look that DoSomething() add text to TextBox1 but ValidationError still displays error message and confuses user.
jlp
A: 

you can use javascript ValidatorEnable function

ValidatorEnable(document.getElementById('<%= ValidatorID.ClientID %>'), true);
Muhammad Akhtar
+1  A: 

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

Bryan