Issue with chaining Required Field and Regular Expression validators for Textbox controls

后端 未结 4 973
失恋的感觉
失恋的感觉 2021-01-19 10:55

I\'m trying to implement a form validation with ASP.net and I have tried every solution suggested here but the best one was on aspsnippets.com so far.

My code is lik

4条回答
  •  佛祖请我去吃肉
    2021-01-19 11:40

    The issue is resolved by replacing the code snippet below. To correct we must loop through all the validators for a control, then we should decide whether it has to marked with the error class. After this, your code will work as expected.

    Replace the loop

     for (var i in Page_Validators) {
        try {
            var control =
            document.getElementById(Page_Validators[i].controltovalidate);
            if (!Page_Validators[i].isvalid) {
                control.className = "error";
            } else {
                control.className = "";
            }
        } catch (e) { }
    }
    

    with the below code

     for (var j in Page_Validators) {
            try {
                var control =
                            document.getElementById(Page_Validators[j].controltovalidate);
                var IsError = false;
                for (var i in control.Validators) {
    
                    if (!control.Validators[i].isvalid) {
                        IsError = true;
                    }
                }
    
                if (IsError)
                    control.className = "error";
                else
                    control.className = "";
            } catch (e) { }
        }
    

    I just ran it and this is working excellently :) Try this solution!

提交回复
热议问题