Im creating a simple registration form in asp.net. Here's my problem: If my confirm passwordfield is empty it still submits the form. I'm using compareToValidate do I also need to use a requiredvalidator. I'm new to asp.net but shouldnt the comparevalidator generate an error if lets say password is 123 and the confirmpassword is empty.
<fieldset>
<legend>Enter your data</legend>
<table>
<tr>
<td align="right">User Name :</td>
<td> </td>
<td align="left">
<asp:TextBox ID="tbUsername" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvUserName" runat="server"
ControlToValidate="tbUsername"
CssClass="ValidationError"
ErrorMessage="« (Required)"
ToolTip="User Name is a REQUIRED field"
></asp:RequiredFieldValidator>
</td>
</tr>`enter code here`
<tr >
<td align="right">Password : </td>
<td> </td>
<td align="left">
<asp:TextBox ID="tbPassword" runat="server" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ErrorMessage="« (Required)"
ControlToValidate="tbPassword"
CssClass="ValidationError"
ToolTip="Password is a REQUIRED field"
></asp:RequiredFieldValidator>
</td>
</tr>
<tr >
<td align="right">Confirm Password : </td>
<td> </td>
<td align="left">
<asp:TextBox ID="ConfirmPass" runat="server" TextMode="Password"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" runat="server"
ControlToValidate="ConfirmPass"
CssClass="ValidationError"
ControlToCompare="tbPassword"
ErrorMessage="No Match"
ToolTip="Password must be the same" />
</td>
</tr>
Step 1 – Open Visual Studio –> Create a new empty Web application. Step 2 – Create a new web page and design web form with three textbox control along with button control. Step 3 – Drag and drop CompareValidator control from Toolbox.
Whenever a user creates a password, there is always one more field of confirm password. It checks that the password entered by the user is same as this confirm password fields. To create a valid password, both the password and confirm password fields value must be matched.
Refer below code. [Required(ErrorMessage = "Password is required." )] [Required(ErrorMessage = "Confirmation Password is required." )] [Compare( "Password" , ErrorMessage = "Password and Confirmation Password must match." )]
The confirm password catches typos by prompting users to type their password twice. While the confirm password field seems sensible, including it can lower your conversion rate.
You must provide a required validator as well! Compare validator will only compare the value with the target control and alert the user.
<td align="left">
<asp:TextBox ID="ConfirmPass" runat="server" TextMode="Password"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" runat="server"
ControlToValidate="ConfirmPass"
CssClass="ValidationError"
ControlToCompare="tbPassword"
ErrorMessage="No Match"
ToolTip="Password must be the same" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ErrorMessage="« (Required)"
ControlToValidate="ConfirmPass"
CssClass="ValidationError"
ToolTip="Compare Password is a REQUIRED field">
</asp:RequiredFieldValidator>
</td>
Hopes this help you!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With