如何在 asp 验证器无效时更改原始 CSS



嗨,我正在尝试在验证器无效时更改我的CSS。我尝试了很多事情,例如在验证器中使用CSSClass更改它,使用JavaScript将其更改,甚至在程序的后端中使用C#。但这似乎不起作用。

这是CSS样式,周围有3 *的部分会导致文本和Buttom Border的颜色变化

<style type="text/css">
.inlog {
  display: block;
  letter-spacing: 4px;
  padding-top: 30px;
  text-align: center;
}
.inlog .invoertext {
  display:block;
  ***color: white;***
  cursor: text;
  font-size: 20px;
  line-height: 20px;
  text-transform: uppercase;
  -moz-transform: translateY(-34px);
  -ms-transform: translateY(-34px);
  -webkit-transform: translateY(-34px);
  transform: translateY(-34px);
  transition: all 0.3s;
}
.inlog .invoertype {
  background-color: transparent;
  border: 0;
  ***border-bottom: 2px solid white;***
  color: white;
  font-size: 36px;
  letter-spacing: -1px;
  outline: 0;
  padding: 5px 20px;
  text-align: center;
  transition: all 0.3s;
  width: 200px;
}
.inlog .invoertype:focus {
  max-width: 100%;
  width: 400px;
}
.inlog .invoertype:focus + .invoertext {
  color: whitesmoke;
  font-size: 13px;
  -moz-transform: translateY(-74px);
  -ms-transform: translateY(-74px);
  -webkit-transform: translateY(-74px);
  transform: translateY(-74px);
}
.inlog .invoertype:valid + .invoertext {
  font-size: 13px;
  -moz-transform: translateY(-74px);
  -ms-transform: translateY(-74px);
  -webkit-transform: translateY(-74px);
  transform: translateY(-74px);
}
</style>

这是我的验证者和文本框

<label id="Signin" class="Loginform">
    <label class="inlog">
        <asp:TextBox class="invoertype" ID="txtNaam" runat="server"></asp:TextBox>
        <asp:Label class="invoertext" runat="server" Text="Voornaam"></asp:Label>
    </label>
    <asp:RequiredFieldValidator
        CssClass="Errormsg"
        runat="server"
        ControlToValidate="txtNaam"
        ErrorMessage="Naam is required"
        ForeColor="Red"
        ValidationGroup="Sigin">
    </asp:RequiredFieldValidator>
    <asp:RegularExpressionValidator
        CssClass="Errormsg"
        runat="server"
        ControlToValidate="txtNaam"
        ErrorMessage="Voer geldige voornaam in"
        ValidationExpression="^[a-zA-Z]{2,}$"
        ForeColor="Red"
        ValidationGroup="Sigin">
    </asp:RegularExpressionValidator>
    <label class="inlog">
        <asp:TextBox class="invoertype" ID="txtemailsignin" runat="server"></asp:TextBox>
        <asp:Label class="invoertext" runat="server" Text="Email"></asp:Label>
    </label>
    <asp:RequiredFieldValidator
        class="Errormsg"
        runat="server"
        ControlToValidate="txtemailsignin"
        ErrorMessage="Email is required"
        ForeColor="Red"
        ValidationGroup="Sigin">
    </asp:RequiredFieldValidator>
    <asp:RegularExpressionValidator
        ID="Errormsgid"
        class="Errormsg"
        runat="server"
        ControlToValidate="txtemailsignin"
        ErrorMessage="Voer geldig email adress in"
        ValidationExpression="^w+([.-]?w+)*@w+([.-]?w+)*(.w{2,6})+$"
        ForeColor="Red"
        ValidationGroup="Sigin">
    </asp:RegularExpressionValidator>
    <asp:CustomValidator
        ID="Signinemailgebuik"
        class="Errormsg"
        runat="server"
        ErrorMessage="Email is al in gebruik"
        ForeColor="Red"
        ValidationGroup="Sigin">
    </asp:CustomValidator>

    <label class="inlog">
        <asp:TextBox class="invoertype" ID="txtPasswordsignin" runat="server" TextMode="Password"></asp:TextBox>
        <asp:Label class="invoertext" runat="server" Text="Password"></asp:Label>
    </label>
    <asp:RequiredFieldValidator
        class="Errormsg"
        runat="server"
        ControlToValidate="txtPasswordsignin"
        ErrorMessage="Password is required"
        ForeColor="Red"
        ValidationGroup="Sigin">
    </asp:RequiredFieldValidator>
    <asp:RegularExpressionValidator
        class="Errormsg"
        runat="server"
        ControlToValidate="txtPasswordsignin"
        ErrorMessage="Password does not meet requirements"
        ValidationExpression="^.*(?=.{6,})((?=.*[!@#$%^&*()-_=+{};:,<.>]){1})(?=.*d)((?=.*[a-z]){1})((?=.*[A-Z]){1}).*$"
        ForeColor="Red"
        ValidationGroup="Sigin">
    </asp:RegularExpressionValidator>
    <label class="inlog">
        <asp:TextBox class="invoertype" ID="txtpasswordsignrepeat" runat="server" TextMode="Password"></asp:TextBox>
        <asp:Label class="invoertext" runat="server" Text="Repeat password">
        </asp:Label>
        <asp:RequiredFieldValidator
            class="Errormsg"
            runat="server"
            ControlToValidate="txtpasswordsignrepeat"
            ErrorMessage="Password is required"
            ValidationGroup="Sigin"
            ForeColor="Red">
        </asp:RequiredFieldValidator>
        <asp:CompareValidator
            class="Errormsg"
            runat="server"
            ControlToValidate="txtpasswordsignrepeat"
            ControlToCompare="txtPasswordsignin"
            Type="String" Operator="Equal"
            ErrorMessage="Password not the same"
            ForeColor="Red"
            ValidationGroup="Sigin"> 
        </asp:CompareValidator>
    </label>
    <label class="buttonplacement">
        <asp:Button ValidationGroup="Sigin" class="btn-draw" runat="server" Text="Sign in" OnClick="Unnamed_Click" />
    </label>
</label>

您在验证器中看到了类和CSSClass的一些不同之处,因为我尝试了不同的选项,如果这会产生差异,但所有选项似乎都不起作用。

图像中的higlig零件是用CSS内部更改的项目,如果验证器无效,我想将其变为红色。

在此处输入图像描述

预先感谢

https://jsfiddle.net/l3cab2hd/

这是我能做的最好的

您可以使用以下片段。它将检查页面上的所有验证器,并根据有效性添加或删除CSS类。

<script>
    $(document).ready(function () {
        ValidatorUpdateIsValid = function () {
            Page_IsValid = AllValidatorsValid(Page_Validators);
            checkValidators();
        }
    });
    function checkValidators() {
        for (i = 0; i < Page_Validators.length; i++) {
            var inputControl = document.getElementById(Page_Validators[i].controltovalidate);
            if (null != inputControl) {
                WebForm_RemoveClassName(inputControl, "validation-summary-errors");
                $("#" + inputControl.id).closest("label").removeClass("validation-summary-errors-label");
            }
        }
        for (i = 0; i < Page_Validators.length; i++) {
            inputControl = document.getElementById(Page_Validators[i].controltovalidate);
            if (null != inputControl && !Page_Validators[i].isvalid) {
                WebForm_AppendToClassName(inputControl, "validation-summary-errors");
                $("#" + inputControl.id).closest("label").addClass("validation-summary-errors-label");
            }
        }
    }
</script>
<style>
    .validation-summary-errors {
        border: 3px solid #ff0000 !important;
    }
</style>

最新更新