Click here to Skip to main content
15,902,114 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to know which the best code for regular expression validation for validation of an email id. The code I use is accepting more than 3 characters at the end.(kumar@gold.com123. I want the last 123 should not be allowed. My code that I use in regular expression validator for emailid is:

XML
<asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server"
            ControlToValidate="TextBox21" ErrorMessage="Enter Valid Email ID "
            style="z-index: 1;width:250px; left: 367px; top: 216px; position: absolute;font-size: x-small;"
            ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>



Please advice.
Posted

A simple google search came up with this[^] link.
 
Share this answer
 
hi,

try this,
HTML
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.(\w+([-.]\w+)*){2,3}"


this will allow, min 2 and max 3 characters after " . "

hope it helps
 
Share this answer
 
v2
Comments
Thomas Daniels 11-Feb-13 4:02am    
For me, your expression returns "true" if I've more than 3 chars after "."
Karthik Harve 11-Feb-13 4:07am    
can you try with updated answer ?
Thomas Daniels 11-Feb-13 4:11am    
The expression returns still true if I've more than 3 chars after "."
There's one presented and discussed here[^]:
^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$

Maybe that's what you're looking for.
 
Share this answer
 
Hi,

This regex works fine for me:
^[^.][a-zA-Z0-9_.-]*[^.]@[^.][a-zA-Z0-9_-]*[^.]\.[a-zA-Z0-9_-]{2,3}$
 
Share this answer
 
Only allowing max 3 characters at the end is not advised since there are addresses now with more 3 characters as the top level domain, e.g. .travel.

Please also keep in mind that the validation is always only a formal one, i.e., you never know from just looking at it if the address really exists.

So, for your example, I would leave that check you ask for away - that's the "best" approach in the sense "not being too strict".

Cheers
Andi
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900