Putting the cursor in text box after validating in ASP.NET - 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"

Related

ASP.NET validator never validates as true

The code below has always worked, but now somehow I can't submit the form anymore even though I provide both username and password. I get no errors in backend or the Chrome Developer Console. I tried enabling/disabling all validators and found that if I add Enabled="false" to validator UserNameRequired, the form does submit. But obviously I want to have this requiredfieldvalidator enabled.
Why is this happening?
You can also see it live here (fill in a random email address/password): http://www.zorgbeurs.nl/login
<table width="300">
<tr>
<td>
<asp:Label ID="UserNameLabel" Text="Email" runat="server" AssociatedControlID="UserName"/>
</td>
<td>
<asp:TextBox ID="UserName" Text="" runat="server" Width="160px"/><br />
<asp:RequiredFieldValidator Enabled="false" ValidationGroup="loginuser" Display="Dynamic" SetFocusOnError="True" ID="UserNameRequired" runat="server" ControlToValidate="UserName" ErrorMessage="*"/>
<asp:RegularExpressionValidator ControlToValidate="UserName" ValidationGroup="loginuser" ErrorMessage="Ongeldig emailadres" ValidationExpression="<%$resources:glossary,regexValidEmail %>" ID="revUsername" runat="server" Display="Dynamic"/>
</td>
</tr>
<tr>
<td valign="top">
<asp:Label ID="PasswordLabel" Text="<%$ Resources:Glossary,Password%>" runat="server" AssociatedControlID="Password"/>
</td>
<td valign="top">
<asp:TextBox ID="Password" runat="server" TextMode="Password" Width="160px"/><br />
<asp:RequiredFieldValidator Enabled="false" SetFocusOnError="True" Display="Dynamic" ValidationGroup="loginuser" ID="PasswordRequired" runat="server" ControlToValidate="Password" ErrorMessage="*" />
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:ImageButton CausesValidation="true" ValidationGroup="loginuser" ID="LoginMember" meta:resourcekey="ibtnLogin" CommandName="Login" runat="server"/>
<asp:CheckBox TextAlign="Right" CssClass="cblist" Font-Bold="false" ID="RememberMe" runat="server" Text="<%$ Resources:Glossary,loginrememberme%>" /><br />
</td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="FailureText" runat="server" ForeColor="Red" Font-Bold="true" EnableViewState="False"/>
</td>
</tr>
</table>
update
I also tried adding the ValidationExpression of revUsername expression directly in the code [-+.'\w]+#[-.\w]+\.[-.\w]+, but that does not help.
I can see the required validators working in your live site without making a postback.
Regarding the email's regular expression validator, it seems that your expression is not working properly. So, I suggest you to change your regular expression, try this one:
<asp:RegularExpressionValidator Enabled="true" ControlToValidate="UserName"
ValidationGroup="loginuser" ErrorMessage="Ongeldig emailadres"
ValidationExpression="\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*"
ID="revUsername" runat="server" Display="Dynamic" />
Also, remember to enable it again in order to make it work both on client and server side.
Hope it can help you!

asp:CreateUserWizard - how to display minimum password requirement

It's not displaying the min requirement until the 'CreateUser' is clicked. How do I make it to validate the password requirement beforehand and display a message when the password field loses focus.
With the default markup of CreateUserWizard control as below, you cannot have such functionality.
<asp:CreateUserWizard ID="CreateUserWizard1" runat="server">
<WizardSteps>
<asp:CreateUserWizardStep ID="CreateUserWizardStep1" runat="server">
</asp:CreateUserWizardStep>
<asp:CompleteWizardStep ID="CompleteWizardStep1" runat="server">
</asp:CompleteWizardStep>
</WizardSteps>
</asp:CreateUserWizard>
Therefore, the idea is to customize the CreateUserWizard control as per your needs. Its better to use Validators so that you can display the messages beforehand. They key is to create a <ContentTemplate> element within the <asp:CreateUserWizardStep> element.
<asp:CreateUserWizardStep ID="CreateUserWizardStep1" runat="server">
<ContentTemplate>
<table>
<tr>
<td>
Sign Up to get started</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">
User Name:</asp:Label></td>
<td>
<asp:TextBox ID="UserName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="UserNameRequired" runat="server"
ControlToValidate="UserName" ErrorMessage="User Name is required."
ToolTip="User Name is required."
ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
</td>
<tr>
<td>
<asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">
Password:</asp:Label></td>
<td>
<asp:TextBox ID="Password" runat="server" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password"
ErrorMessage="Password is required." ToolTip="Password is required."
ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
</td>
</tr>
</ContentTemplate>
</asp:CreateUserWizardStep>
Read MSDN for a complete tutorial.

2 validators validate one at a time

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

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

ASP.NET: RegularExpressionValidator Doesn't reCheck the input

i have a RegularExpressionValidator to validate an Email input that i have,
it works perfectly, if the input matches the Regular-expression,
However,if i enter a mistaken email it would show an error msg, if i fix the email in the input , it doesnt recheck it, the error msg stays not allowing me to click the Registeration button-or more like it gets clicked, but no event gets fired-
EDIT: added the ASPX code
< table width="100%">< tr><td>Username:</td>
<td>
<input runat="server" id="txtUsername" type="text" size="30" />
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server"
ControlToValidate="txtUsername" ErrorMessage="*"></asp:RequiredFieldValidator
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server"
ErrorMessage="Username has to be atleast between 4-8 "
ValidationExpression="[A-Z]{4-8}"
ControlToValidate="txtUsername"></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td> Email:</td><td>
<input runat="server" id="txtemail" type="text" size="30" /></td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="txtemail" ErrorMessage="*">
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ControlToValidate="txtemail" ErrorMessage="Format must be: abc#abc.com"
ValidationExpression="^[A-Za-z0-9._%+-]+#([A-Za-z0-9-]+\.)+([A-Za-z0-9]{2,4}|museum)$">
</asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td>
<asp:Label runat="server" ForeColor="red" ID="lblerror"></asp:Label>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button runat="server" Text="Register"
ID="btnSubmit" Width="118px" onclick="btnSubmit_Click" />
</td>
</tr>
</table>
Are you checking for Page.IsValid in your btnSubmit_Click method?
Also your validation expression for username doesn't look correct, use a comma between your min and max lengths.
ValidationExpression="[A-Z]{4-8}"
ValidationExpression="[A-Z]{4,8}"

Resources