01-18-2005 10:56 AM chris

VB6 Code to Validate Email Address

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

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

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

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

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

Cheers! xD

Filed under:

# re: VB6 Code to Validate Email Address

Tuesday, January 18, 2005 8:09 AM by chris

Ah por cierto... este codigo no es mio, por algun lado lo encontré en mis doctos y lo subo para que lo puedan usar

# re: VB6 Code to Validate Email Address

Wednesday, April 09, 2008 10:50 PM by Bhupinder Singh

Thanks

# re: VB6 Code to Validate Email Address

Thursday, May 08, 2008 5:44 AM by Nilantha

thank you for posing a valuable validation procedure for all VB6 community. Appreciate your work...

# re: VB6 Code to Validate Email Address

Thursday, July 10, 2008 6:03 AM by sumwun

Sucks big. Old VB6 Ninja Style. Wohoo. To the spanish guy: Did not understand one word.

# re: VB6 Code to Validate Email Address

Friday, September 26, 2008 8:44 AM by xxbomb

thnx for the code..:)

really appreciate it.^^

# re: VB6 Code to Validate Email Address

Saturday, March 07, 2009 3:55 PM by joe

THANKS! :D

you're the best!

# re: VB6 Code to Validate Email Address

Tuesday, May 12, 2009 7:39 AM by Freakss

Thank you dude

right code for the job

# re: VB6 Code to Validate Email Address

Thursday, August 13, 2009 10:33 PM by snatarajan

thanks for your value code

# re: VB6 Code to Validate Email Address

Thursday, August 13, 2009 10:34 PM by natarajan

thanks for your value code  in email validation

# re: VB6 Code to Validate Email Address

Thursday, January 21, 2010 9:57 PM by Sanaullah

Thanks dear for sharing your valuable effort

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Protected by FormShield
Refresh
Listen
Please enter the characters shown on the image


Code: