17/12/2012

VB.Net code for validating Email in a text box

There is several code for validating an email address enter in a text box and on this subject I already written a post and included a simple code for that but that code was just for beginner but here I am mentioning vb.code that is very useful for validating  email.  Just create a sub procedure and and call that sub procedure. the code for the sub procedure will be as under. For better understanding of these type of vb.net code or database related inquiry the book written by Mike Murach is very good book which is available on amazon with free shipping charge, I will suggest one can  refer


Public Sub Email_Leave(ByVal sender As Object, ByVal e As System.EventArgs)
Dim txtEmail_1 As TextBox = sender
f txtEmail_1.Text <> "" Then
Dim rex As Match = Regex.Match(Trim(txtEmail_1.Text), "^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,3})$", RegexOptions.IgnoreCase)
If rex.Success = False Then
MessageBox.Show("Check your Email-Address 1", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
txtEmail_1.BackColor = Color.blue
txtEmail_1.Focus()
Exit Sub
Else
txtEmail_1.BackColor = Color.White
End If
End If
End Sub