|
-
Apr 10th, 2006, 04:12 AM
#1
Thread Starter
Addicted Member
Valadaing an email address in a text box
Does any one have any sample code on how i can Valadate an email address. Thanks
-
Apr 10th, 2006, 04:18 AM
#2
Re: Valadaing an email address in a text box
You'd use regular expressions to validate it something like this:
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
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.
-
Apr 10th, 2006, 04:22 AM
#3
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
-
Apr 10th, 2006, 10:29 AM
#4
Thread Starter
Addicted Member
Re: Valadaing an email address in a text box
hi, i cant get this to work it says Regex is not declared ?
-
Apr 10th, 2006, 12:28 PM
#5
Frenzied Member
Re: Valadaing an email address in a text box
try adding this to the top of the page:
VB Code:
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|