Ram Singh
Ram Singh

Reputation: 6938

How to Validate Email Address using RegularExpression validator?

I have used the below code to validate the email address:

<asp:RegularExpressionValidator ID="EmailAddressFormatValidator" runat="server" 
     ControlToValidate="EmailAddressTextBox" 
     ErrorMessage="RegularExpressionValidator" 
     Display="Dynamic" 
     ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" 
     EnableClientScript="False">Email Address is not valid.<br />       
</asp:RegularExpressionValidator>

It is working fine, but when users enters " " (space) in the end of the email, it stops working and declare the valid email to invalid, please help me how i can resolve this.

Upvotes: 0

Views: 9348

Answers (2)

Anubhav Chaudhary
Anubhav Chaudhary

Reputation: 133

Just use this:

ValidationExpression="\s*\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*\s*" in place of your Validation Expression.

Similar questions can be found here:

http://www.c-sharpcorner.com/forums/

Upvotes: 0

aelor
aelor

Reputation: 11126

add \s* towards the end of the regex you have

like this

\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*\s*

i have set up a demo here

http://regex101.com/r/tX7mA8

the regex I have used here is ^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*\s*$

I have not much about your language so I suppose those anchor tags are present there for you by default

Upvotes: 5

Related Questions