Atur layout sebagai berikut:
Kita akan menggunakan Regular Expression, maka tahap awal adalah code import:
Imports System.Text.RegularExpressions
Pertama untuk validasi 1 alamat email. Buat fungsi sebagai berikut:
Private Shared Function SingleEmailValid(ByVal EmailAddress As _
String) As String
Dim regex As Regex = New Regex("([a-zA-Z0-9_\-\.]+)@" & _
"((\[[0-9]{1,3}\.[0-9]{1,3}\." & _
"[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+" & _
"\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})", _
RegexOptions.IgnoreCase _
Or RegexOptions.CultureInvariant _
Or RegexOptions.IgnorePatternWhitespace _
Or RegexOptions.Compiled _
)
If EmailAddress.Trim.Length = 0 Then
Return "Email Address must be filled."
ElseIf regex.IsMatch(EmailAddress) And _
EmailAddress.Equals(regex.Match(EmailAddress).ToString) _
Then
Return ""
Else
Return "Invalid email Address."
End If
End Function
Kemudian panggil function tersebut pada event Form2_Load dan TextBox1_TextChanged
Private Sub Form2_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Me.ErrorProvider1.SetError(Me.TextBox1, _
SingleEmailValid(Me.TextBox1.Text))
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles TextBox1.TextChanged
Me.ErrorProvider1.SetError(Me.TextBox1, _
SingleEmailValid(Me.TextBox1.Text))
End Sub
Run debug dan coba masukan beberapa karakter pada textbox.
Click here if you like this article.
Baca Juga: VB.NET: Email Validation (Multiple Email)
4 Comments
'errorprovider1' is not a member of ....
Coba ditambahkan, cari di toolbox bagian components.
http://1.bp.blogspot.com/_61c_vpZbWyk/SKM4q4fM7OI/AAAAAAAAAI4/huFlIf1O6yM/s1600-h/toolbox-components-error-provider.png