user3355957
user3355957

Reputation: 47

Multiple ASP.NET validators on the same control - Page_Validator.isValid always remains true

I am using this JavaScript code in order to highlight textboxes and other controls (on which ASP.NET validators are specified) after validation fails.

<script src="_scripts/jquery-1.6.4.min.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function() {
        HighlightControlToValidate();
        $('#<%=btnSave.ClientID %>').click(function() {
            if (typeof (Page_Validators) != "undefined") {
                for (var i = 0; i < Page_Validators.length; i++) {
                    if (!Page_Validators[i].isvalid) {
                        $('#' + Page_Validators[i].controltovalidate).css("background", "#f3d74f");
                    }
                    else {
                        $('#' + Page_Validators[i].controltovalidate).css("background", "white");
                    }
                }
            }
        });
    });

    function HighlightControlToValidate() {
        if (typeof (Page_Validators) != "undefined") {
            for (var i = 0; i < Page_Validators.length; i++) {              
                $('#' + Page_Validators[i].controltovalidate).blur(function() {
                var validatorctrl = getValidatorUsingControl($(this).attr("ID"));                               
                    if (validatorctrl!= null && !validatorctrl.isvalid) {
                        $(this).css("background", "#f3d74f");
                    }
                    else {
                        $(this).css("background", "white");
                    }
                });
            }
        }
    }   
    function getValidatorUsingControl(controltovalidate) {      
        var length = Page_Validators.length;
        for (var j = 0; j < length; j++) {
            if (Page_Validators[j].controltovalidate == controltovalidate) {
                return Page_Validators[j];
            }
        }
        return null;
    }   
</script>

This script works fine whenever there is no more than one ASP.NET validator specified on the same control. Unfortunately, if there are multiple ASP.NET validators specified on the same control and validation fails, the control in question is not highlighted.

After debugging using the Chrome developer tools, I found out that the isValid property of the ASP.NET validators in question is always set to "true" whenever there are multiple ASP.NET validators specified on the same control, even if validation fails. If the ASP.NET validators on the same control are reduced to one validator, the isValid property is set to "false" when validation fails, as it should be.

Why is this happening please? How can I solve this? Thanks.

Upvotes: 1

Views: 951

Answers (1)

David
David

Reputation: 1774

If I've read that dense code correctly, your code is looping through each validator, then getting the first validator for the associated control.

So one control might have 3 validators, and then for each of those three validators, it will get the same control and then the first validator for that control.

Instead of using:

getValidatorUsingControl($(this).attr("ID"))

just use:

$('#' + Page_Validators[i])[0]

Actually, that won't work because i will be out of scope or have a changed value at the time when the field is blurred, but do something along those lines.

Upvotes: 1

Related Questions