在文本框

时间:2015-10-06 10:29:53

标签: c# asp.net

我在asp.net c#application

的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文本框中输入错误的密码后会显示。

2 个答案:

答案 0 :(得分:0)

当您将属性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文本框,这很好。

答案 1 :(得分:0)

只需整理代码,试试这个:

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