2

Below is my html code. I have a email textbox and there is a login button. I have added a required field validator and regular expression validator for email textbox.

The problem is that when I type some thing in the email textbox browser's auto suggestion shows some list of emails. When I select any of those emails by using down arrow key and enter key it shows the error message for regular expression validation even though email is in proper format.

<asp:RequiredFieldValidator ID="reqValUserName" runat="server" 
    ErrorMessage="Email is required!" 
    ControlToValidate="txtUserName"
    ValidationGroup="validateCredential"
    Display="Dynamic">
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="regValUserName" runat="server" 
    ErrorMessage="Incorrect format!"
    ControlToValidate="txtUserName" 
    ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" 
    ValidationGroup="validateCredential"
    Display="Static">
</asp:RegularExpressionValidator>                 

     <asp:TextBox ID="txtUserName" runat="server"
           TabIndex="1" CssClass="inputCredential" MaxLength="60"
           AccessKey="E" 
           ValidationGroup="validateCredential">
     </asp:TextBox>

     <asp:Button ID="btnLogin" runat="server" CssClass="btnPrimary" 
           Text="Login" onclick="btnLogin_Click" 
           ValidationGroup="validateCredential"/>

enter image description here

In this image as you see if I select the email from the suggestion and press enter it is showing the wrong email validation message.

Can anyone please let me know, how to stop this kind of message display?

If there is any clarification needed regarding the question then please add it as a comment.

4
  • Where is your regular expression validator Commented Jul 31, 2012 at 5:51
  • Sorry, missed to mention the regular expression validator. Check the edited question. Commented Jul 31, 2012 at 5:54
  • Is there a leading whitespace in the email suggestion? Commented Aug 2, 2012 at 16:04
  • So you want to get rid of the client side validation and only use server side valdiation when the button is clicked? Commented Aug 3, 2012 at 0:48

6 Answers 6

2
+50

You could add the EnableClientValidation="false" attribute to the regex validator so that it only checks the format on the server after the other validators have been passed.

Or follow the advice here:

What determines the order validators fire in?

Sign up to request clarification or add additional context in comments.

Comments

1

Also add regular expression validator for email text box

<asp:RegularExpressionValidator ID="regtxtPrimaryEmail" runat="server" ControlToValidate="txtEmailId"
                                            Display="Dynamic" CssClass="cssVal" ToolTip="Invalid email." ValidationGroup="registration"
                                            ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>

Then it will not submit until the email format correct.

4 Comments

By mistake did not add regular expression validation in this question. Now changed the question. Problem is not validation. Problem is showing validation message wrongly.
yes, validation is working. but the error is showing only when selecting from suggestion. :(
i am not asking for validation. i am asking if you are using some other email then you are able to successfully reach to your goal or you are getting same error there also..
Then might be your regular expression is wrong. Please change it and then try again...
1

It seems your some controls has autopostback="true" and your these controls are in update panel if not then what you can do is..Remove the display properties of all the validation controls and on btnLogin_Click Event call Validate(); Method.

Also read more In Depth detail on Validators on MSDN

you'll certainly get your answer....

3 Comments

I have already tried autopostback="false". But its not working. Also I cant call Validate() as this needs an postback.
i didn't mean that...validations don't need postback that's why they are there for. Valdiate() we use to make explicit call to validators from client side...Try setting Display property of every validation control to Dynamic and let us know...other wise you should try Client side validation for example check this link c-sharpcorner.com/uploadfile/purankaushal/103222006013805am/…
+1 Thanks for giving the idea regarding validating page in click event handler. But this doesn't solve the purpose exactly. Because if the validation fails I dont want to check whether the inputs are correct or not. So I am using Page.IsValid and accordingly checking credential's correctness.
1

Thanks for all your answers and suggestions. Below is what I have done after going through all the answers.

<asp:RegularExpressionValidator ID="regValUserName" runat="server" 
     ErrorMessage="Incorrect format!"
     ControlToValidate="txtUserName" 
     ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" 
     ValidationGroup="validateCredential"
     Display="Dynamic" EnableClientScript="false">
</asp:RegularExpressionValidator>

As you can see, I have added EnableClientScript="false" so that the error message will not be shown when I type half of email and select from auto suggestion and press enter.

Now the problem was it was always checking for whether entered credentials are correct of not as it was doing validation in server side. So I had some unnecessary code execution and database interaction.

So in click event handler of login button I did following change.

if(Page.IsValid)
{
     // My credential check code
}

So the above block of code will run code for checking correctness of entered credentials only if they are in proper format.

But I am still looking for a better answer. This is only a work around. Because when it comes to performance server side validation can never match client side validation. Here I am compromising with usability. As I want user to be notified immediately after he/she enters a wrong formatted email. This can be achieved by using javascript, but I wonder if there is any way we can achieve it using validator controls..

Comments

0

This is happening because the client-side REV is validating on the partial input. For example, in the above illustration, the REV is validating "r" as its input. In order to verify this,

  1. type in the entire email address "[email protected]" >> then
  2. select the suggested email using the down arrow >> then
  3. hit the enter key.

The REV will not complain this time.

As for the solution: implement the REV in javascript. Add a label next to the textbox for error message. Call the js when the cursor exits the textbox. If the validation fails, find the label in the js and add the error message.

2 Comments

Thanks for your suggestion. But is there any way we can stop the partial input validation?
The workaround I've suggested should work. But as far as manipulating the RFV control is concerned, I don't believe there is much to be done, since the events timing logic and the order in which they are fired is abstracted.
0

Just see the properties of the validator there you will find 'Display' property under Appearance section, set it to dynamic and VOLA!!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.