2

Is there away to call a javascript function after a validation control fails validation?

2 Answers 2

2

Check article "ASP.NET Validation in Depth" from MSDN - section of particular interest is Client Side Validation that provides client side API. You can use isvalid property of a validator object to decide if its valid or not. Validator objects (on client side) can be referred using ClientID property of server side validator control.

One of the way to achieve what you want can be

  1. Turn off ASP.NET validation by setting js variable Page_ValidationActive to false
  2. When post-back happens iterate via all validators using Page_Validators property and validate each calling ValidatorValidate.
  3. If validator of interest in not valid then call your function.
Sign up to request clarification or add additional context in comments.

Comments

2

There is an undocumented way. It depends on Microsoft not changing the name of their javascript methods used for client side validation, but fortunately it degrades gracefully, meaning it won't crash your site if Microsoft changes something.

Bare minimum you need to store a pointer to the original function then overwrite the function that Microsoft is calling.

var pointerToMicrosoftValidator = ValidatorUpdateIsValid;
ValidatorUpdateIsValid = function() {
    pointerToMicrosoftValidator();
    // do something after Microsoft finishes 
}

Since you only want to do something where you fail validation you should check if the page is valid after the return from the call:

var pointerToMicrosoftValidator = ValidatorUpdateIsValid;
ValidatorUpdateIsValid = function() {
    pointerToMicrosoftValidator();
    if (Page_IsValid) {
        alert("Passed Validation");
    } else {
        alert("Failed Validation");
    }
    // do something after Microsoft finishes 
}

I found it was important to test that the validator was being used on the page I thought it was, in case someone on my team removed the validator without removing my javascript. So I added a check:

if (window.ValidatorUpdateIsValid) {
    alert("page with validator");
}

Finally, I wanted to make sure my function was created after Microsoft created their code so I included the definition in a jquery.ready call. I can then call a method after the validation by replacing the "alert after". You should remove all the alerts before going live with this.

$(document).ready(function() {
    //intercept microsoftValidator
    if (window.ValidatorUpdateIsValid) {
        alert("page with validator");
        var pointerToMicrosoftValidator = ValidatorUpdateIsValid;
        ValidatorUpdateIsValid = function() {
            alert("before");
            if (window.pointerToMicrosoftValidator) {
                pointerToMicrosoftValidator ();
                if (Page_IsValid) {
                    alert("Passed Validation");
                } else {
                    alert("Failed Validation");
                }
            }
            alert("after");
        }
    }
});

2 Comments

I've tried this solution but it not only didn't work but also disabled the validation. After overriding Page_ClientValidate function like they did here: stackoverflow.com/a/7692484/1813219 and not ValidatorUpdateIsValid it worked like a charm.
Great! could be they changed the undocumented way I described in the intervening 5 years between our posts. Glad you found a new better solution.

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.