2 validators validate one at a time - asp.net

Hi I have a change password page.
I have 2 textboxes, New Password and Confirm Password.
Both have a requiredFieldValidators.
And a compareValidator.
But what happens is when I key in the first field New Password, the second textbox's requiredFieldValidator and CompareValidator are showing error messages at the same time.
I only want to show requiredFieldValidator and only when that validator returns true, I want to validate compareValidator.
How can I achieve that. The following is my code.
<table class="editAccTable">
<tr>
<td>
New Password<br />
<asp:TextBox ID="txtNewPwd" runat="server" TextMode="Password" Width="204px"></asp:TextBox>
<br />
<asp:RequiredFieldValidator ID="rfvNewPwd" runat="server"
ControlToValidate="txtNewPwd"
ValidationGroup="vgChangePwd"
Display="Dynamic"
></asp:RequiredFieldValidator>
</td>
<td>
Confirm New Password<br />
<asp:TextBox ID="txtConfPwd" runat="server" TextMode="Password" Width="204px"></asp:TextBox>
<br />
<asp:RequiredFieldValidator ID="rfvConfPwd" runat="server"
ControlToValidate="txtConfPwd"
ValidationGroup="vgChangePwd"
Display="Dynamic"
></asp:RequiredFieldValidator>
<asp:CompareValidator ID="cvChangePwd" runat="server"
ValidationGroup="vgChangePwd"
ControlToValidate="txtNewPwd"
ControlToCompare="txtConfPwd"
ValueToCompare="String"
Display="Dynamic"
></asp:CompareValidator>
</td>
</tr>
<tr>
<td colspan="2">
<asp:ImageButton ID="ibtnPwdChange" runat="server"
ImageUrl="/images/submit.jpg" onclick="ibtnPwdChange_Click"
ValidationGroup="vgChangePwd"
Width="90px" Height="36px"
/>
<br />
<asp:Label ID="lblSuccessPwdChange" runat="server"></asp:Label>
</td>
</tr>
</table>

There is a couple of work around.
For my case, I can remove the requiredFieldValidator for the second textbox as it is enough to have only compareValidator.
But there will be some cases, that there must have 2 validators. In that case, we can use css to overlap the 2 validators.

Try CompareValidator like this
<asp:CompareValidator id="cvChangePwd" runat="server" ControlToValidate="txtConfPwd" ControlToCompare="txtNewPwd" ErrorMessage="Your passwords do not match up!" Display="Dynamic" />

set display property as dynamic
display="dynamic" in all the validators

Related

Field Validator

In the code below, the submit buttion has the 'SubmitValidation' ValidationGroup.
When i change the name of the ValidationGroup in the RequiredFieldValidator (to submitValidation123), the Error Message 'Enter State Code' does not appear (as expected). However, the submit does not take place till I actually enter a value for the state code. WHY?
<tr>
<td>
<span class="requiredText">*</span> State Code (Ex. TX):
</td>
<td>
<asp:TextBox runat="server" ID="statecodeTxt" Width="100px"></asp:TextBox>
<asp:RequiredFieldValidator ID="statecodeRFV" runat="server"
Display="Dynamic" ValidationGroup="submitValidation"
ControlToValidate="statecodeTxt" SetFocusOnError="True"
ErrorMessage="Enter State Code" />
</td>
</tr>
<asp:Button ID="submitBtn" Text="Submit" disabled="true"
runat="server" ValidationGroup="submitValidation"
OnClick="submitBtn_Click" />
Try remove or make false the
SetFocusOnError="True"

Can I have RequiredFieldValidator to check for more than one ControlToValidate?

I am having 3 TextBoxes in a row and then a Next button.
On clicking Next button, the control would go to the next page only if ALL of the 3 TextBoxes contain some user-entered text.
Is there any option by which I can check whether all of the TextBoxes contain some text by using RequiredFieldValidator (rather than going for different RequiredFieldValidator for each ControlToValidate)?
I am currently having following code:
<tr>
<td class="style1">
<asp:Label ID="lblDOB" runat="server" Font-Bold="True" Font-Italic="False" Font-Size="Medium"
ForeColor="Black" Text="Date of Birth"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtA" runat="server" Width="45px"></asp:TextBox>
<asp:TextBox ID="txtB" runat="server" Width="45px"></asp:TextBox>
<asp:TextBox ID="txtC" runat="server" Width="45px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator9" runat="server"
ControlToValidate="txtA"
ErrorMessage="Please enter Text">
</asp:RequiredFieldValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidator10" runat="server"
ControlToValidate="txtB"
ErrorMessage="Please enter Text">
</asp:RequiredFieldValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidator11" runat="server"
ControlToValidate="txtC"
ErrorMessage="Please enter Text">
</asp:RequiredFieldValidator>
</td>
</tr>
So, instead of having 3 different RequiredFieldValidators, I want to use only a single RequiredFieldValidator which checks if all the 3 TextBoxes contain some text. How can I achieve this?
Your best bet is to use CustomValidator and combine with either client side javascript or server side c#/vb.net code.
You can't validate more than one control using RequiredFieldValidator.
you can't use the RequiredFieldValidator with more than one control ,
but i prefer to use the HTML5 required attribute instead of ASP.NET Validation.
<asp:TextBox ID="txt1" required="required" />

Validation summary AutoUpdate

In my asp.net application, I've tried to use validation summary in my web page.I've controls like
<td>First Name *</td>
<td>
<asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
<div>
<asp:RequiredFieldValidator ID="reqValStaffFirstName" runat="server"
ErrorMessage="Enter FirstName" Text="*"
ControlToValidate="txtFirstName" Display="Dynamic"
ValidationGroup="valGrpStaffGeneral">
</asp:RequiredFieldValidator>
</div>
</td>
<td>Last Name *</td>
<td>
<asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
<div>
<asp:RequiredFieldValidator ID="reqValStaffLastName" runat="server"
ErrorMessage="Enter LastName" Text="*"
ControlToValidate="txtLastName" Display="Dynamic"
ValidationGroup="valGrpStaffGeneral">
</asp:RequiredFieldValidator>
</div>
</td>
<td>
<asp:Button Id="btnSave" runat="server" ValidationGroup="valGrpStaffGeneral">
<td>
and my validationsummary script is
<asp:ValidationSummary ID="summary1" ShowSummary="true" runat="server"
ValidationGroup="valGrpStaffGeneral" DisplayMode="BulletList" />
If I click on save button without filling anything in textbox1 and textbox2 its showing validation errors in my validation summary area. If I fill anything in my textboxes and press tab the error(*) disappears automatically, but the error in the validation summary not changing automatically and its displaying until I click on save button, what should I do to make the validation summary change the fixed errors automatically.
This might be of help - look at the EnableClientScript property of the ValidationSummary control.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.validationsummary.enableclientscript.aspx

Putting the cursor in text box after validating in ASP.NET

I have a basic ASP.NET website which has a register page with the user name, password and email as text boxes. Only one of them, the email, has a validation which is the default email validation from ASP.NET.
After the validation, I want the cursor to be in the email textbox if the validation fails.
Currently the message is displayed but the cursor is not on the page.
<tr>
<td>
<asp:TextBox ID="txtPassword" runat="server" CssClass="cpTextBox" TextMode="Password"
TabIndex="7" ToolTip="Password must be at least six characters in length"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvPassword" runat="server" ControlToValidate="txtPassword"
ErrorMessage="Please enter password" Display="None">*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="revPassword" runat="server" ControlToValidate="txtPassword"
ErrorMessage="Password must be at least six characters in length" ValidationExpression=".{6,50}"
Display="None">*</asp:RegularExpressionValidator>
</td>
<td>
<asp:TextBox ID="txtTitle" runat="server" CssClass="cpTextBox" TabIndex="11"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblVerifyPassword" runat="server" CssClass="cpLabel" Text="Verify password"></asp:Label>
<asp:Label ID="Label4" runat="server" ForeColor="Red" Text="*"></asp:Label>
</td>
<td>
<asp:Label ID="lblPhone" runat="server" CssClass="cpLabel" Text="Phone"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:TextBox ID="txtVerifyPassword" runat="server" CssClass="cpTextBox" TextMode="Password"
TabIndex="8"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvConfirmPassword" runat="server" ControlToValidate="txtVerifyPassword"
ErrorMessage="Please verify the password" Display="None">*</asp:RequiredFieldValidator>
<asp:CompareValidator ID="cvVerifyPassword" runat="server" ControlToCompare="txtPassword"
ControlToValidate="txtVerifyPassword" ErrorMessage="Please verify the password"
Display="None">*</asp:CompareValidator>
</td>
Can someone guide me on how to do this?
Regards.
For the validator control please set SetFocusOnError="true"

ASP.NET - Required Field Validator giving false negative errors on Listbox

Background: I have a bundles listbox that inherits values from the carriers listbox once a carrier is selected via a web service.
I have a validationGroup on the button, I used Page.IsValid on the click handler and it says "Nothing".
When i select different carriers in IE8 it resets the other form values but not in IE9.
With Autopost=false on the lbCarriers, the Bundles listbox wont load any data.
With CausesValidation="true" in "lbCarriers", Bundles listbox wont load any data either Do you know how to do that w Ajax?
Do you know how I could do this w/ Ajax?
Problem: Using the required field validator on the bundles listbox is returning an a false erorr when I have bundles selected. When I click the Send Button, I get the "Select At Least 1 Bundle" Error Message but the invitation still sends out an i get an email.
Here's a screenshot of the application:
asp.net code on default.aspx page:
<tr>
<td class="style5">
Carrier:<br />
<font size="1">*Hold Ctrl Key Down to Select Multiple Carriers</font></td>
<td bgcolor="#ffffff" class="style7">
<asp:ListBox ID="lbCarriers" SelectionMode="Multiple" AutoPostBack="true"
runat="server" Height="86px" Width="250px" ValidationGroup="ValidationGroup">
</asp:ListBox>
</td>
<td bgcolor="#ffffff" class="style2">
<asp:RequiredFieldValidator ID="CarrierValidator" runat="server" Text="*"
ErrorMessage="Select At Least 1 Carrier" ControlToValidate="lbCarriers"
ValidationGroup = "ValidationGroup" ForeColor="Red" ></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="style1">
Bundles:<br />
<font size="1">*Hold Ctrl Key Down to Select Multiple Bundles</font></td>
<td bgcolor="#ffffff" class="style6">
<asp:ListBox ID="bundles" SelectionMode="Multiple" runat="server" Height="86px"
Width="250px" Enabled="True"
ValidationGroup="ValidationGroup" CausesValidation="True">
</asp:ListBox>
</td>
<td bgcolor="#ffffff" class="style2">
<asp:RequiredFieldValidator ID="BundleValidator" runat="server" Text="*"
ErrorMessage="Select At Least 1 Bundle" ControlToValidate="bundles"
ValidationGroup = "ValidationGroup" ForeColor="Red" ></asp:RequiredFieldValidator>
</td>
</tr>
<asp:Button ID="Send_Button" runat="server"
Text="Send Invitation" ValidationGroup="ValidationGroup" Width="123px"/>
<br />
<asp:Label ID="Send_Success" runat="server" Text="Invitation sent!" Visible="false"></asp:Label>
<br />
<asp:ValidationSummary ID="ValidationSummary" runat="server" ForeColor="Red"
ValidationGroup="ValidationGroup" />
Question: What alternate code or work-around do you recommend for this issue?
Thanks for looking!
EDIT:
Add CausesValidation="true" in "lbCarriers"
I have removed Autopost="true" from first listbox i.e. "lbCarriers" and it is working now.

Resources