ASP.NET: RegularExpressionValidator Doesn't reCheck the input - asp.net

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

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!

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"

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"

validation showing in both panels in asp.net

i am using two panels (login and registration) in asp.net where in both panels i used button control. and i also used validations for particular textboxes in both panels.
i want to show both panels at same time.
now what problem i am facing is that when i click on button of login panel, it shows validation error text in registration panel and vice-versa..
Is there nay other way to use any other control instead of button.
i am using visual studio-2008
i had created login panel as:
<asp:Panel ID="pnllogin" runat="server">
<table class = "style1">
<tr>
<td>
<asp:Label ID="lblloginid" runat="server" Text="Login_ID" Font-Bold="true"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtboxloginid" runat="server" ></asp:TextBox>
<asp:RequiredFieldValidator ID="requiredfieldvalidator1" runat="server"
ControlToValidate="txtboxloginid" ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblpassword" runat="server" Text="Password" Font-Bold="true"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtboxpassword" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="requiredfieldvalidator2" runat="server"
ControlToValidate="txtboxpassword" ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td></td>
<td>
<asp:HyperLink ID="hyperlinkforgotpassword" runat="server" ForeColor="Red" NavigateUrl="~/ForgotPassword.aspx">Forgot Password??</asp:HyperLink>
</td>
</tr>
<tr>
<td> </td>
<td>
<asp:ImageButton ID="imgbtnlogin" runat="server" ImageUrl="~/images/login.png"
onclick="imgbtnlogin_Click" />
</td>
</tr>
</table>
</asp:Panel>
and somewhat same code for registration panel..
can someone please help me..
Make use of ValidationGroup property to fire the validation message related to proper panel
here is detail info : http://www.w3schools.com/ASPNET/prop_webcontrol_imagebutton_validationgroup.asp
demo : http://www.w3schools.com/ASPNET/showasp.asp?filename=demo_prop_webcontrol_imagebutton_validationgroup
Specifying Validation Groups

ValidationSummary doesn't show up

I have a few validation controls with a validationsummary.
The client side validations shows up, after the user leaves the textbox for the validation.
The server side validation shows never up.
The Validationsummary shows never up.
What I am doing wrong?
<div class="designPhotoMiddleText" id="MiddleReg" >
<asp:Panel DefaultButton="linkRegister" runat="server" ID="panRegister" >
Jetzt kostenlos registrieren:<br />
<br />
<table>
<tr>
<td style="width: 120px;">
Username:
</td>
<td>
<asp:TextBox ID="txtRegisterUsername" Width="150px" runat="server"></asp:TextBox>
<asp:CustomValidator ValidationGroup="Register" ID="valUsername" ControlToValidate="txtRegisterUsername"
OnServerValidate="IsUsernameFree" CssClass="validator"
runat="server" ErrorMessage="CustomValidator"></asp:CustomValidator>
</td>
</tr>
<tr>
<td>
Passwort:
</td>
<td>
<asp:TextBox ID="txtRegisterPW1" Width="150px" TextMode="Password" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ValidationGroup="Register" CssClass="validator" ControlToValidate="txtRegisterPW1" ID="valPasswordLenght" runat="server" ErrorMessage="Das Passwort muss mindestens 6 Zeichen haben."></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td>
Passwort erneut:
</td>
<td>
<asp:TextBox ID="txtRegisterPW2" Width="150px" TextMode="Password" runat="server"></asp:TextBox>
<asp:CompareValidator CssClass="validator" ValidationGroup="Register"
ID="valPW" ControlToCompare="txtRegisterPW2" ControlToValidate="txtRegisterPW1" runat="server" ErrorMessage="Die eingegebenen Passwörter stimmen nicht überein."></asp:CompareValidator>
</td>
</tr>
<tr>
<td>
Email:
</td>
<td>
<asp:TextBox ID="txtRegisterEmail" Width="150px" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ValidationGroup="Register" CssClass="validator" ForeColor="black"
ControlToValidate="txtRegisterEmail" ID="valMail" runat="server"
ValidationExpression=".*#.*\.(com|net|org|edu|mil|at?|de?|ch?|uk?)$"
ErrorMessage="Bitte geben Sie eine gültige EMail-Adresse ein."
Display="Dynamic"></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:CheckBox ID="chkRegsiterAGBs" runat="server" />
<asp:HyperLink ID="linkAGB" Target="_blank" NavigateUrl="~/AGBs.aspx" runat="server">AGBs</asp:HyperLink> gelesen
<asp:CustomValidator ValidationGroup="Register" ID="valAGB"
OnServerValidate="IsAGBChecked" CssClass="validator"
runat="server" ErrorMessage="CustomValidator"></asp:CustomValidator>
</td>
</tr>
<tr>
<td colspan="2">
<asp:LinkButton CausesValidation="true" ID="linkRegister" CssClass="linkWhite" runat="server"
onclick="linkRegister_Click">Jetzt Registrieren</asp:LinkButton>
<asp:ValidationSummary ValidationGroup="Register" ID="sumRegister" runat="server"
HeaderText="Fehler:"
ShowSummary="true" DisplayMode="BulletList" />
</td>
</tr>
</table>
</asp:Panel>
</div>
public void IsUsernameFree(object source, ServerValidateEventArgs value)
{
string username = value.Value;
DAL.User user = DAL.UserHandling.GetUserByName(username);
value.IsValid = (user == null);
}
public void IsAGBChecked(object source, ServerValidateEventArgs value)
{
value.IsValid = (chkRegsiterAGBs.Checked);
}
Can't tell for sure from looking at the code you've posted, but are you checking the Page's IsValid property anywhere? E.g. before running any other methods?
if (Page.IsValid){
//Do additional processing
//register user etc.
}
this check will force execution of all validation controls on the page and should then trigger the display of your validation errors.
EDIT:
You need to set the ValidationGroup to 'Register' in your markup that defines the linkRegister control
`<asp:LinkButton CausesValidation="true" ID="linkRegister" CssClass="linkWhite" runat="server" OnClick="linkRegister_Click" ValidationGroup="Register">Jetzt Registrieren</asp:LinkButton>`
Also, your RegularExpressionValidator for password needs to have its ValidationExpression property set in the markup:
`<asp:RegularExpressionValidator ValidationGroup="Register"
CssClass="validator" ControlToValidate="txtRegisterPW1"
ID="valPasswordLenght" runat="server" ErrorMessage="Das Passwort
muss mindestens 6 Zeichen haben." Validationexpression="[\w+\d+]{6,}"
</asp:RegularExpressionValidator>`

Resources