Howto make RequiredFieldValidator change the css class of the parent div

后端 未结 3 1037
伪装坚强ぢ
伪装坚强ぢ 2020-12-20 15:14

I have a TextBox input element that has a RequiredFieldValidator as such:

相关标签:
3条回答
  • 2020-12-20 15:49

    Use the following:

    <script>
    
        $(function(){
            if (typeof ValidatorUpdateDisplay != 'undefined') {
                var originalValidatorUpdateDisplay = ValidatorUpdateDisplay;
    
                ValidatorUpdateDisplay = function (val) {
                    if (!val.isvalid) {
                        $("#" + val.controltovalidate).closest("div").addClass("form-error");
                    }
    
                    originalValidatorUpdateDisplay(val);
                }
            }
        });
    
    </script>
    

    This code decorates the original ValidatorUpdateDisplay function responsible for updating the display of your validators, updating the controltovalidate closest div.

    0 讨论(0)
  • 2020-12-20 15:52

    I think you may find the below option useful. Basically it overrides one of ASP.NET validations default scripts. And shoves some logic into it to add / remove error class on parent div.

    The problem I had faced with this task was getting my script to execute after validations were triggered since the DOM is not reloaded. By overriding this the original validation script you have the ability to run your own script when the validation display is updated.

    This way you can continue to use your other validation controls. You do have to place the script at the bottom of your page or masterpage. I also used JQuery to make things simpler.

    *Note the below code requires your validators be set to display dynamic. If you don't want them to be visible you can add a css class to them and set display: none.

    <script type="text/javascript">
        function ValidatorUpdateDisplay(val) {
            if (typeof (val.display) == "string") {
                if (val.display == "None") {
                    return;
                }
                if (val.display == "Dynamic") {
                    val.style.display = val.isvalid ? "none" : "inline";
                    return;
                }
            }
            if ((navigator.userAgent.indexOf("Mac") > -1) && (navigator.userAgent.indexOf("MSIE") > -1)) {
                val.style.display = "inline";
            }
            val.style.visibility = val.isvalid ? "hidden" : "visible";
    
    
            //---  Add / Remove Error Class
            var triggeredSiblings = 0;
            if (val.isvalid) {
                $(val).siblings('span').each(function () {
                    if ($(this).isvalid == false) { triggeredSiblings = 1; }
                });
                if (triggeredSiblings == 0) { $(val).closest("div").removeClass("error"); }
            } else {
                $(val).closest("div").addClass("error");
            }
        }
    </script>
    
    0 讨论(0)
  • 2020-12-20 16:01

    with RequiredFieldValidator you can not add your custom code.

    you should use asp.net customvalidator control, and write your own custom validation javascript function which sets the class to the div.

    0 讨论(0)
提交回复
热议问题