Chad
Chad

Reputation: 561

Javascript checking if page is valid

On my submit button, what I'd like to do is OnClick show a "Please wait" panel and hide the button, UNLESS the validators say something's invalid - then I need the buttons still showing obviously. Otherwise I have a validation summary showing erros and no way to submit again.

Most articles I find about doing this want to use Page_ClientValidate() function to tell the page to validate itself, but this comes back undefined for me, as does Page_IsValid variable. Here is the function I'm trying to use - what am I missing?:

function PleaseWaitShow() {
    try {
        alert("PleaseWaitShow()");

        var isPageValid = true;

        // Do nothing if client validation is not active
        if (typeof(Page_Validators) == "undefined") {
            if (typeof(Page_ClientValidate) == 'function') {
                isPageValid = Page_ClientValidate();
                alert("Page_ClientValidate returned: " + isPageValid);
                alert("Page_IsValid=" + Page_IsValid);
            } else {
                alert("Page_ClientValidate function undefined");
            }
        } else {
            alert("Page_Validators undefined");
        }

        if(isPageValid) {
            // Hide submit buttons
           document.getElementById('pnlSubmitButton').style.visibility = 'hidden';
           document.getElementById('pnlSubmitButton').style.display = 'none';

           // Show please wait panel
           document.getElementById('pnlPleaseWait').style.visibility = 'visible';
           document.getElementById('pnlPleaseWait').style.display = 'block';
       } else {
           alert("page not valid - don't show please wait");
       }
   } catch(er) {
       alert("ERROR in PleaseWaitShow(): " + er);
   }
}

Upvotes: 8

Views: 21733

Answers (6)

Zeeshan Ali
Zeeshan Ali

Reputation: 341

Just check

if(Page_IsValid)
{
    //Yourcode
}

This works if you have validators in the page, which excludes the validation summary.

Upvotes: 4

jrummell
jrummell

Reputation: 43077

There's an ASP.Net forum thread on this topic: Button that prevents multiple clicks

Here's the solution (in code behind):

private void BuildClickOnceButton(WebControl ctl)
{
    System.Text.StringBuilder sbValid = new System.Text.StringBuilder();
    sbValid.Append("if (typeof(Page_ClientValidate) == 'function') { ");
    sbValid.Append("if (Page_ClientValidate() == false) { return false; }} ");
    sbValid.Append(ctl.ClientID + ".value = 'Please wait...';");
    sbValid.Append(ctl.ClientID + ".disabled = true;");
    //GetPostBackEventReference obtains a reference to a client-side script function that causes the server to post back to the page.
    sbValid.Append(ClientScript.GetPostBackEventReference(ctl, ""));
    sbValid.Append(";");
    ctl.Attributes.Add("onclick", sbValid.ToString());
}

Upvotes: 0

Rick
Rick

Reputation:

change this line "if (typeof(Page_Validators) == "undefined") " to if (typeof(Page_Validators) != "undefined")

Upvotes: 8

Chad
Chad

Reputation: 561

I believe I've found a "kind of" answer.

I still cannot identify why my page will not identify "Page_ClientValidate()" or "Page_IsValid" - this part is still unanswered.

However, I am using a number of PeterBlum validators on the page, and those do provide a "VAM_ValOnSubmit()" that returns true/false. So this may be the solution. I might just have to be sure all the validators are PeterBlum to catch them all.

Not the greatest solution, but better than I've gotten so far. I'm still open to answers on the "Page_IsValid" portion.

Upvotes: 0

Zhaph - Ben Duguid
Zhaph - Ben Duguid

Reputation: 26956

According to the section "The Client-Side API" on the page "ASP.NET Validation in depth":

Page_IsValid | Boolean variable | Indicates whether the page is currently valid. The validation scripts keep this up to date at all times.

Indeed, watching this variable in FireBug on a form with ASP.NET client side validation enabled, it does get updated as I fill in details of the form (incorrectly, or correctly).

Obviously, if you've disabled client script on your validators or the validation summary, then this variable won't be available to you.

Upvotes: 5

mkoryak
mkoryak

Reputation: 57928

Page_ClientValidate() is not any standard javascript function i know of

Upvotes: 0

Related Questions