开发者

Calling javascript after a .net validator fails

Is there away to call a javascript function after a validation control fails validati开发者_JAVA百科on?


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.


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");
        }
    }
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜