none
How to validate an email id in a textbox?

    Question

  • Can anyone please give me a sample code to validate email id in a textbox?

    Thank you in advance

    Friday, March 24, 2017 9:01 PM

Answers

  • Hi

    Maybe this will help

    Option Strict On
    Option Explicit On
    Option Infer Off
    Imports System.Net.Mail
    Public Class Form1
        Private Sub TextBox1_Validated(sender As Object, e As EventArgs) Handles TextBox1.Validated
            Try
                Dim emailAddress As MailAddress = New MailAddress(TextBox1.Text)
                MessageBox.Show("Email Address validated")
            Catch ex As FormatException
                MessageBox.Show(ex.Message)
            Catch ex As ArgumentNullException
                MessageBox.Show(ex.Message)
            Catch ex As ArgumentException
                MessageBox.Show(ex.Message)
            End Try
        End Sub
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by Tabzee Sunday, March 26, 2017 5:47 PM
    Friday, March 24, 2017 10:37 PM

All replies

  • It can be done by the ValidateEmail api, import by ordinal number 6 from msoert2.dll (Mail RT Lib)

    It returns 0 if OK.

    (tested on Windows 10)

    • Edited by Castorix31 Friday, March 24, 2017 9:53 PM
    Friday, March 24, 2017 9:51 PM
  • Hi

    Maybe this will help

    Option Strict On
    Option Explicit On
    Option Infer Off
    Imports System.Net.Mail
    Public Class Form1
        Private Sub TextBox1_Validated(sender As Object, e As EventArgs) Handles TextBox1.Validated
            Try
                Dim emailAddress As MailAddress = New MailAddress(TextBox1.Text)
                MessageBox.Show("Email Address validated")
            Catch ex As FormatException
                MessageBox.Show(ex.Message)
            Catch ex As ArgumentNullException
                MessageBox.Show(ex.Message)
            Catch ex As ArgumentException
                MessageBox.Show(ex.Message)
            End Try
        End Sub
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by Tabzee Sunday, March 26, 2017 5:47 PM
    Friday, March 24, 2017 10:37 PM
  • Can anyone please give me a sample code to validate email id in a textbox?

    Thank you in advance

    If you want to check that a given e-mail address is correctly formed then have a look here:

    https://msdn.microsoft.com/en-us/library/01escwtf%28v=vs.100%29.aspx

    For example:

    Imports System.Globalization
    Imports System.Text.RegularExpressions

    Public Class RegexUtilities Dim invalid As Boolean = False public Function IsValidEmail(strIn As String) As Boolean invalid = False If String.IsNullOrEmpty(strIn) Then Return False ' Use IdnMapping class to convert Unicode domain names. strIn = Regex.Replace(strIn, "(@)(.+)$", AddressOf Me.DomainMapper) If invalid Then Return False ' Return true if strIn is in valid e-mail format. Return Regex.IsMatch(strIn, _ "^(?("")(""[^""]+?""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" + _ "(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-\w]*[0-9a-z]*\.)+[a-z0-9]{2,17}))$", RegexOptions.IgnoreCase) End Function Private Function DomainMapper(match As Match) As String ' IdnMapping class with default property values. Dim idn As New IdnMapping() Dim domainName As String = match.Groups(2).Value Try domainName = idn.GetAscii(domainName) Catch e As ArgumentException invalid = True End Try Return match.Groups(1).Value + domainName End Function End Class


    If you mean that you want to check to see if it's actually an active e-mail address, that's entirely different. There are services that will do that (for example, via SOAP), but finding something for free will be a challenge.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Friday, March 24, 2017 11:03 PM
  • Thank you everyone for their responses!!!!

    It really helped me.

    Sunday, March 26, 2017 5:47 PM
  • Frank, that link is outdated.  The current link is:

    https://msdn.microsoft.com/en-us/library/01escwtf(v=vs.110).aspx

    Sunday, March 26, 2017 11:54 PM
  • Frank, that link is outdated.  The current link is:

    https://msdn.microsoft.com/en-us/library/01escwtf(v=vs.110).aspx


    Thanks :)

    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Sunday, March 26, 2017 11:56 PM