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

时间:2019-03-14 07:27:36

标签: css asp.net

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

这是CSS样式,周围带有3 *的部分会导致文本和底部边框的颜色发生变化

<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>

您会在验证器中看到class和CSSClass的一些差异,因为我尝试了diffrent选项,如果这样做会产生差异,但所有选项似乎都无法正常工作。

图像中被忽略的部分是那些在Css中用颜色更改的项目,如果验证程序无效,我想将其更改为红色。

enter image description here

预先感谢

https://jsfiddle.net/L3cab2hd/

这是我能做的最好的事情,但是看起来却不起作用

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码段。它将检查页面上的所有验证器,并根据有效性添加或删除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>