Results 1 to 5 of 5

Thread: Valadaing an email address in a text box

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    164

    Valadaing an email address in a text box

    Does any one have any sample code on how i can Valadate an email address. Thanks

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Valadaing an email address in a text box

    You'd use regular expressions to validate it something like this:
    VB Code:
    1. Private Const EMAIL_PATTERN As String = "^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"
    2.  
    3.     Private Sub emailText_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles emailText.Validating
    4.         Dim address As String = Me.emailText.Text.Trim()
    5.  
    6.         'Check that the e-mail address field is either empty or contains a valid e-mail address.
    7.         If address <> String.Empty AndAlso Not Regex.IsMatch(address, EMAIL_PATTERN) Then
    8.             Me.emailText.HideSelection = False
    9.             Me.emailText.SelectAll()
    10.  
    11.             MessageBox.Show("Please enter a valid e-mail address.", _
    12.                             Application.ProductName, _
    13.                             MessageBoxButtons.OK, _
    14.                             MessageBoxIcon.Error)
    15.  
    16.             Me.emailText.HideSelection = True
    17.             e.Cancel = True
    18.         End If
    19.     End Sub
    That's taken from a project I'm working on at this very second. Note that that pattern is relatively basic. It will allow things like "[email protected]". If you truly want to validate an e-mail address stringently, the O'Reilly book "Mastering Regular Expressions" provides a pattern to do it that is more than a page long.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: Valadaing an email address in a text box

    This works properly for me
    ^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    164

    Re: Valadaing an email address in a text box

    hi, i cant get this to work it says Regex is not declared ?

  5. #5
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Cool Re: Valadaing an email address in a text box

    try adding this to the top of the page:
    VB Code:
    1. Imports System.Text.RegularExpressions
    ~Peter


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width