在文本框中输入密码前,会出现确认密码的提示

本文关键字:密码 确认 提示 文本 输入 | 更新日期: 2023-09-27 18:16:17

我在asp.net c#应用程序的aspx页面中编写了以下代码

<section>
      <label>
         <asp:Label ID="lblPassword" runat="server" Text="Password *"></asp:Label></label>
      <label class="input">
                <asp:TextBox ID="txtPassword" TextMode="Password" runat="server" data-content="Please Enter Your Password?" data-rel="popover" data-original-title="Password"></asp:TextBox>
                <asp:RequiredFieldValidator ID="rfvPassword" runat="server" Display="Dynamic" CssClass="state-error" ValidationGroup="profile" SetFocusOnError="true"
                    ControlToValidate="txtPassword">please enter password</asp:RequiredFieldValidator>
            </label>
        </section>
        <section>
            <label>
                <asp:Label ID="lblConfirmPassword" runat="server" Text="Confirm Password *"></asp:Label></label>
            <label class="input">
                <asp:TextBox ID="txtConfrimPassword" TextMode="Password" runat="server" data-content="Please Confirm Your Password?" data-rel="popover" data-original-title="Confrim Password"></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" Display="Dynamic" ValidationGroup="profile" SetFocusOnError="true"
                    CssClass="state-error" ControlToValidate="txtConfrimPassword">Please re-enter your password</asp:RequiredFieldValidator>
                <asp:CompareValidator runat="server" ID="valComPassword" CssClass="state-error" ControlToValidate="txtPassword" ControlToCompare="txtConfrimPassword"
                    Display="Dynamic" ValidationGroup="profile" SetFocusOnError="true" ErrorMessage="Confirm password does not match"></asp:CompareValidator>
            </label>
        </section>

现在的问题是当用户在txtPassword文本框中输入密码时,验证(确认密码)出现在txtConfirmPassword文本框下方。我想它应该出现后,用户将在txtConfirm文本框中输入他们的错误密码。

在文本框中输入密码前,会出现确认密码的提示

当您将属性ControlToCompare设置为txtConfrimPassword并开始在txtPassword文本框中键入此验证失败,因为当您键入txtPassword文本框时,确认密码文本框为空。

因此您可以简单地验证密码文本框而不是确认文本框。

<asp:CompareValidator runat="server" ID="valComPassword"  CssClass="state-error"  
    ControlToValidate="txtConfrimPassword" ControlToCompare="txtPassword" 
    Display="Dynamic" ValidationGroup="profile" SetFocusOnError="true" 
    ErrorMessage="Confirm password does not match">
</asp:CompareValidator>

这将不会抛出错误消息,直到你开始输入txtConfrimPassword文本框,这是好的。

整理你的代码,试试这个:

<section>
    <asp:Label ID="lblPassword" runat="server" Text="Password *"></asp:Label>
    <asp:TextBox ID="txtPassword" TextMode="Password" runat="server" data-content="Please Enter Your Password?" data-rel="popover" data-original-title="Password"></asp:TextBox>
    <asp:RequiredFieldValidator ID="rfvPassword" runat="server" Display="Dynamic" CssClass="state-error" ValidationGroup="profile" SetFocusOnError="true" ControlToValidate="txtPassword">please enter password</asp:RequiredFieldValidator>
</section>
<section>
    <asp:Label ID="lblConfirmPassword" runat="server" Text="Confirm Password *"></asp:Label>
    <asp:TextBox ID="txtConfrimPassword" TextMode="Password" runat="server" data-content="Please Confirm Your Password?" data-rel="popover" data-original-title="Confrim Password"></asp:TextBox>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" Display="Dynamic" ValidationGroup="profile" SetFocusOnError="true" CssClass="state-error" ControlToValidate="txtConfrimPassword">Please re-enter your password</asp:RequiredFieldValidator>
    <asp:CompareValidator runat="server" ID="valComPassword" CssClass="state-error" ControlToValidate="txtPassword" ControlToCompare="txtConfrimPassword" Display="Dynamic" ValidationGroup="profile" SetFocusOnError="true" ErrorMessage="Confirm password does not match"></asp:CompareValidator>
</section>