0

I have a setup similar to this:

<asp:TextBox ID="txtEmail" runat="server"/>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
    ControlToValidate="txtEmail" ErrorMessage="Required field cannot be left blank."
    Display="Dynamic"/>

<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
    ErrorMessage="Invalid email address."    ControlToValidate="txtEmail" 
    ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" 
    Display="Dynamic"/>

Now, I'm running some sort of js code which will change the value of the textbox, and I'd like to force the validators to trigger again. The JS code I'm using is intended to be modular, so when it triggers I don't know the name of the validation group, whether the control has a validator attached to it or whatever.

I've ended up using

Page_ClientValidate()

But the page itself is going to have more than one validator, and I don't want them all the pop up at the same time.

So the basic question is - is there a way that I can get a list of validators 'related' to a control, and force them to revalidate the input?

(To clarify - the code I posted above is just to explain my problem better. It's not the actual code I'm using)

1 Answer 1

1

You can associate the email validators with a validation group (for example "emailValidationGroup") :

<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
    ControlToValidate="txtEmail" ErrorMessage="Required field cannot be left blank."
    Display="Dynamic" ValidationGroup="emailValidationGroup"/>

<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
    ErrorMessage="Invalid email address."    ControlToValidate="txtEmail" 
    ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" 
    Display="Dynamic" ValidationGroup="emailValidationGroup"/>

and then call : Page_ClientValidate("emailValidationGroup");

Sign up to request clarification or add additional context in comments.

2 Comments

The javascript I'm writing is intended to be modular. I don't know at writing-time the name of the validation group. The intention is that this code will be 'plug in' and will work.
If you know the name of the textbox where you are changing the value, doesn't that tell you what validation group to trigger?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.