Does any one have any sample code on how i can Valadate an email address. Thanks
Printable View
Does any one have any sample code on how i can Valadate an email address. Thanks
You'd use regular expressions to validate it something like this: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.VB Code:
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})(\]?)$" Private Sub emailText_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles emailText.Validating Dim address As String = Me.emailText.Text.Trim() 'Check that the e-mail address field is either empty or contains a valid e-mail address. If address <> String.Empty AndAlso Not Regex.IsMatch(address, EMAIL_PATTERN) Then Me.emailText.HideSelection = False Me.emailText.SelectAll() MessageBox.Show("Please enter a valid e-mail address.", _ Application.ProductName, _ MessageBoxButtons.OK, _ MessageBoxIcon.Error) Me.emailText.HideSelection = True e.Cancel = True End If End Sub
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]$
hi, i cant get this to work it says Regex is not declared ?
try adding this to the top of the page:VB Code:
Imports System.Text.RegularExpressions