VB6 Code to Validate Email Address

Esta función permite validar un correo electrónico en Microsoft Visual Basic 6 SP5.

1 Public Function IsValidEmail(strEmail As String) As Boolean
2 Dim names, name, i, c
3 IsValidEmail = True
4
5 names = Split(strEmail, «@»)
6
7 If UBound(names) <> 1 Then
8 IsValidEmail = False
9 Exit Function
10 End If
11
12 For Each name In names
13
14 If Len(name) <= 0 Then
15 IsValidEmail = False
16 Exit Function
17 End If
18
19 For i = 1 To Len(name)
20 c = LCase(Mid(name, i, 1))
21
22 If InStr(«abcdefghijklmnopqrstuvwxyz_-.», c) <= 0 And Not IsNumeric(c) Then
23 IsValidEmail = False
24 Exit Function
25 End If
26 Next
27
28 If Left(name, 1) = «.» Or Right(name, 1) = «.» Then
29 IsValidEmail = False
30 Exit Function
31 End If
32
33 Next
34
35 If InStr(names(1), «.») <= 0 Then
36 IsValidEmail = False
37 Exit Function
38 End If
39
40 i = Len(names(1)) – InStrRev(names(1), «.»)
41
42 If i <> 2 And i <> 3 Then
43 IsValidEmail = False
44 Exit Function
45 End If
46
47 If InStr(strEmail, «..») > 0 Then
48 IsValidEmail = False
49 Exit Function
50 End If
51
52 End Function

Ya en el código fuente de tu app mandas a llamar la función de la siguiente manera:

1 Private Sub Text1_LostFocus()
2 Dim Valid_Email As Boolean
3
4 Valid_Email = IsValidEmail(Text1.Text )
5
6 If Valid_Email = False Then ‘If the variable is false
7 ‘Message the user to enter appropriate Email address
8 MsgBox «Please enter a valid Email address», vbInformation, «Error»
9 Text1.SetFocus ‘Set focus back to control
10 End If
11
12 End Sub

O bien podría tambien ser en el evento Text1_Validate pero para efectos de demo, lo dejamos así.

Cheers! xD


Comentarios

Deja una respuesta