在asp.net中验证发生时,如何使TextBox边框变为红色?

时间:2017-04-22 12:27:45

标签: asp.net validation

This what i want to do.click to view photo

我厌倦了许多选择,但都没有奏效。喜欢重写JS方法和自定义方法。我将感谢一个明确的,有效的回应。

<tr>
    <td class="auto-style2">&emsp;<asp:Label ID="Label2" runat="server" Text="Name"
        CssClass="style3" AssociatedControlID="TextBoxName"></asp:Label>
    </td>
    <td>
        <asp:TextBox ID="TextBox2" runat="server"
            CssClass="TextBox"></asp:TextBox>
    </td>
    <td>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator3"
            runat="server" ErrorMessage="Field required"
            ControlToValidate="TextBoxName" Text="Required"
            ForeColor="Red" Font-
            Strikeout="False" Font-Underline="False" Font-
            Overline="False" Font-Bold="True" BackColor="Red">
        </asp:RequiredFieldValidator>
    </td>
    <td>&nbsp;</td>
</tr>

1 个答案:

答案 0 :(得分:1)

这是我用于此目的的脚本。

<script type="text/javascript">

    //set the interval for checking the validators
    setInterval(function () { colorBorders() }, 100);

    function colorBorders() {
        if (typeof (Page_Validators) !== 'undefined') {

            //loop all the validators
            for (var i = 0; i < Page_Validators.length; i++) {
                var validator = Page_Validators[i];
                var control = document.getElementById(validator.controltovalidate);

                //check if the control actually exists
                if (control != null) {

                    //if the validator is not valid color the border red, if it is valid return to default color
                    //elseif with rgb color is nessecary for preventing chrome dropdown blinking
                    if (!validator.isvalid) {
                        control.style.borderColor = '#ff0000';
                    } else if (control.style.borderColor == "rgb(255, 0, 0)") {
                        control.style.borderColor = '#000000';
                    }
                }
            }
        }
    }
</script>