ISSUE :
 
Profile photo
Submitted by G-Stone
on May 13th, 2011 at 08:58:10 EST

How to validate email in Visual Basic .net

Hi,
I need to validate email address using VB.NET. I am kind of new to VB.NET world. So, this question may be trivial to most. :)
Thanks in advance.


Profile photo
Replied by SBajra
on May 13th, 2011 at 09:07:13 EST
You can use regular expression to validate email. Regular expression would work in any language, not just VB.NET.

For example, you can do something like this to validate email:
This returns boolean, true or false

Dim txtEmail As String = "someone@somecompany.com"

If Regex.IsMatch(txtEmail, "^(?("")("".+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$") Then
MessageBox.Show("valid email")
Else
MessageBox.Show("Invalid email")
End If


The point is to use this regular expression:
"^(?("")("".+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$
Accepted Answer
Accepted
 Answer

Profile photo
Replied by SBajra
on May 13th, 2011 at 09:08:50 EST
This is the regular expression:
^(?("")("".+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$

Profile photo
Replied by G-Stone
on May 13th, 2011 at 10:15:43 EST
Thanks.

Profile photo
Replied by Antonio Sarc
on April 13th, 2012 at 03:28:50 EST
here is the full vb.net source code of validate eamil address.

http://net-informations.com/vbprj/communications/email-validation.htm

sarc.