Regular Expression for Email Address validation in C#
The task at hand is to validate the Textbox in asp.net for a valid email address.
We can attach a RegularExpressionValidator to the text box with the following regular expression:
<asp:RegularExpressionValidator ValidationExpression="^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$" ID="rxpEmail" runat="server"
ErrorMessage="Email Address is not valid" Text="*" Display="Static" ControlToValidate="txtEmail"></asp:RegularExpressionValidator>
UPDATES
Well i searched the net for the standards of a valid email id and i came across with a wonderful article at
I Knew How To Validate An Email Address Until I Read The RFC
It gives a an idea of what an exact RFC standard for the email id is.
I believe that we should follow validations which are quite obvious to the users and allow only standard email addresses.
Read more post on ASP.NET 2.0 C#.NET 2.0 Microsoft
Comments
I don’t understand the right part of the regex.
It would match:
name1.name2@mail.123
I don’t think tld can be numbers.
i think we can have email ids of type
abc@cmpnyname.local1
“+” is also valid character.
e.g.
yourname+thispartgetsignored@gmail.com
is equivalent to:
yourname@gmail.com
new info for me….
have never tried this…i will work on it…
thnkss david…
I prefer a dumbed down the expression that catches most of the real-world problems instead of verifying every possible combination. The ultimate validation is when the mail server rejects the address…then you know for sure!
Our Republic
Micah u r rite as the ultimate validation for a email address is achieved when we ping the mail server for the specified email address…
Hi, I could see one nice method here http://www.Tctcworld.com. if u need go through it..
If you want to validate e-mail addresses by pinging their mail server just use EmailVerify.NET, a .NET component which can test e-mail syntactically, verify the DNS MX records for the each domain and connect to each mail server to check if the mailbox exists or not.
It works like a charm!!
There’s also a nice demo on their website: http://www.emailverify.net
Hope this helps.
This worked for my task
[a-z]*\.*[a-z]*@[a-z]*\.[a-z]*
Trackbacks