You can use the following code snippet to validate an email address.
Create a new windows application of vb.net and place a textbox named textbox1 and a button. on the click of that button paste the following code
ChkValidEmail(TextBox1.Text)
' Now paste the following code
Public Function ChkValidEmail(ByVal Value As String, Optional ByVal MaxLength As Integer = 255, Optional ByVal IsRequired As Boolean = True) As Boolean If Value Is Nothing OrElse Value.Length = 0 Then Return Not IsRequired ElseIf Value.Length > MaxLength Then Return False End If If Not System.Text.RegularExpressions.Regex.IsMatch(Value, "^[-A-Za-z0-9_@.]+$") Then MsgBox(" Invalid character in the e-mail address. Only(A-Z,a-z,_,0-9,@,.)is valid. ") TextBox1.Clear() TextBox1.Focus() Return False End If ' search the @ char Dim i As Integer = Value.IndexOf("@"c) ' there must be at least three chars after the @ If i <= 0 Or i >= Value.Length - 3 Then MsgBox("INVALID E-Mail: '@' missing.") TextBox1.Clear() TextBox1.Focus() Exit Function End If ' ensure there is only one @ char If Value.IndexOf("@"c, i + 1) >= 0 Then MsgBox(" INVALID E-Mail: '@'Cann't be allowed twice.") TextBox1.Clear() TextBox1.Focus() Exit Function End If ' check that the domain portion contains at least one dot Dim j As Integer = Value.LastIndexOf("."c) ' it can't be before or immediately after the @ char If j < 0 Or j <= i + 1 Then MsgBox(" INVALID E-Mail: '.' missing. ") TextBox1.Clear() TextBox1.Focus() Exit Function End If ' if we get here the address if validated MsgBox(" Congratulations Operator:- Entered E-Mail Address is valid. ") Return True End Function
Summary
By this code u can easily validate the e-mail entered in the textbox
|
| Author: Rajesh Panda 22 Nov 2004 | Member Level: Bronze Points : 0 |
Dear Friends, There is small bug in this program.That is after successful validation of the email address the address has been cleared.so just ignore the last 2 lines of the code.
|
| Author: Babu Aboobacker 23 Mar 2005 | Member Level: Silver Points : 0 |
I have got a simple Regex string from MSDN for validating email id.
private bool IsValidEmail(string strIn) { // Return true if strIn is in valid e-mail format. return System.Text.RegularExpressions.Regex.IsMatch(strIn, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"); }
|