Alex Rivera | Logout

Can I run some custom script after ASP.NET client side page validation fails?

Asked 2011-10-07T19:58:37.397
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>
Edit
Report

1 Answer

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();" ... />
answered 2011-10-07T20:07:27.163

Your Answer