14

I'm trying to run some client side script if, and only if, the client side Page validation fails, and can't figure out where I can hook it in.

If i bind my JavaScript function to the OnClientClick of the button that submits the form, it runs before the client side validation. If I bind it to the OnSubmit of the form, that only fires if the validation passes.

Any ideas of how or where I can hook something like this up? Or if you have other suggestions, I'm open to them.

<form id="frm" runat="server" 
     onsubmit="FUNCTION HERE WONT FIRE IF VALIDATION FAILS">
<asp:requiredfieldvalidator id="vld" runat="server" controltovalidate="txt"/>
<asp:textbox id="txt" runat="server"></asp:textbox>

<asp:button id="cmd" runat="server" OnClick="dosomething"
     OnClientClick="FUNCTION FIRES BEFORE VALIDATION OCCURS">

</form>

3 Answers 3

16

Add script below at the end of page's markup file:

var originalValidationFunction = Page_ClientValidate;
if (originalValidationFunction && typeof (originalValidationFunction) == "function") {
    Page_ClientValidate = function (validationGroup) {
        originalValidationFunction(validationGroup);

        if (!Page_IsValid) {
            // your code here
            alert("oops!");
        }
    };
}
Sign up to request clarification or add additional context in comments.

4 Comments

I like this better than the accepted answer cause it doesn't force me to control the validation myself, only piggyback afterward.
This is the best solution I found.
Not working for me. I can see in the debugger it only gets called with validationGroup set to empty string, not being called for other validation groups, Page_IsValid is true when it reaches the if and then becomes false as you exit the function.
Plus I don't think this code is correct. Page_ClientValidate returns true or false, the overriding function should return the value returned by originalValidationFunction
14

Try using Page_ClientValidate("") to trigger validation from JavaScript, and then you can run some custom code:

validate = function(){
    var isValid = Page_ClientValidate(""); //parameter is the validation group - thanks @Jeff
    if (isValid){
        isValid = somethingToCheck();
    }
    return isValid;
}

<asp:Button ID="Button1" runat="server" CausesValidation="false" OnClientClick="return validate();" ... />

5 Comments

Beat me to it! @Doozer, the parameter is the validation group so if you are using one, you'll need to pass that in: Page_ClientValidate("MyValidationGroup")
Ah... this looks like the right path. In your example, would I set Button1 to CausesValidation="false", or will that matter?
@DoozerBlake: Hmm, if you don't want validation to key off until you call Page_ClientValidate("") then I would say YES to setting CausesValidation="false", tentatively.
It doesn't seem to matter either way. I tried it with both, and OnClientClick appears to fire off before the Validation happens. I appreciate the help.
Just don't miss the part where you put in the validationGroup to the Page_ClientValidate parameter.
2

So you have two options how to handle it:

  1. Use CustomValidator validator which provides ClientValidationFunction feature and inside your custom validation function obviously you know whether validation failed. It gives you as much as you need flexibility on client side validation by accepting a JavaScript function to be used whilst validation.

  2. Check validator satus by accessing it via JavaScript by accessing validator's isValid property from JavaScript (+jQuery):

 var anonymousValidator = $("#<%= vldCommentText.ClientID %>")[0];
 ValidatorEnable(anonymousValidator, true);
 if (!anonymousValidator.isvalid) 
 {
     // ...
 }

3 Comments

I'm aware of CustomValidator's, but how would I know within this CustomValidator function if every other validator on the page is valid? The example above uses one field, but the form I have in question has 20+ validators on it. Would I have to check their status first?
@Doozer Blake : good point which you've to mention in the original question. SO to check status of a multiple validators Validation Groups feature is in place. See Nice article by Scott Gu: weblogs.asp.net/scottgu/archive/2004/10/24/246945.aspx
Sorry I wasn't more specific about that. Thank you for your help though.

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.