The small application below show you how to validate email address in textbox using VB.Net. To test process, I have designed as below:
No | Control Type | Name | Text |
1 | Form1 | ValidateEmail | Validate Email |
2 | Textbox1 | TextBoxEmail | |
3 | Label1 | LabelStatus | |
4 | Button1 | ButtonValidate | Validate Email |
This process, I used ButtonValidate_Click to check email address in TextBoxEmail. I use to condition which is True/False condition as you see in code below and the result will show in LabelStatus. This code work propertly!!!
Code
Imports System.Text.RegularExpressions
Public Class ValidateEmail
Private Sub ButtonValidate_Click(sender As Object, e As EventArgs) Handles
ButtonValidate.Click
Dim pattern As String = "^[a-z][a-z|0-9|]*([_][a-z|0-9]+)*([.][a-z|0-9]+([_][a-z|0-9]+)*)?@[a-z][a-z|0-9|]*\.([a-z][a-z|0-9]*(\.[a-z][a-z|0-9]*)?)$"
Dim match As System.Text.RegularExpressions.Match = Regex.Match(TextBoxEmail.Text.Trim(),
pattern, RegexOptions.IgnoreCase)
If (match.Success) Then
LabelStatus.Text = "The email address :
" & TextBoxEmail.Text
& "
is corrected"
Else
LabelStatus.Text = "The email address :
" & TextBoxEmail.Text
& "
is incorrected !!!"
TextBoxEmail.Clear()
End If
End Sub
End Class
0 comments:
Post a Comment