Link to home
Start Free TrialLog in
Avatar of dwe0608
dwe0608Flag for Australia

asked on

Conversion of Vb.net Function

Hi guys

I am looking for the following function to be converted from VB.net to vb6

Function ValidateEmail(ByVal email As String) As Boolean
    Dim emailRegex As New System.Text.RegularExpressions.Regex( 
        "^(?<user>[^@]+)@(?<host>.+)$")
    Dim emailMatch As System.Text.RegularExpressions.Match = 
       emailRegex.Match(email)
    Return emailMatch.Success
End Function

Open in new window


I do not kno vb.net and do not know if this function can be converted and I would appreciate a fully detailed explanation of how the conversion can be explained.

MTIA

DWE
Avatar of louisfr
louisfr

VB6 does not have regular expressions capabilities, but that simple one would translate easily into VB code. Since the function only checks for success, you don't even need to do a full translation: it will return true for any input which incudes a '@' character.

Function ValidateEmail(ByVal email As String) As Boolean
    ValidateEmail = Instr(email, "@")
End Function

Open in new window

VB6 does not have regular expressions capabilities...
I seem to recall that you can import the VBScript regex library into a VB6 application. (Haven't worked in VB6 in years, so I may be off my rocker.)

it will return true for any input which incudes a '@' character.
Including:

test@test@.com
test@@test.com
@@@@

Suffice it to say, probably not that simple  ; )
That function does nothing else than checking the existence of the @ character.
Translating it doesn't need to import anything.

Sure, the original version is a poor e-mail validating algorithm.
ASKER CERTIFIED SOLUTION
Avatar of Joe Howard
Joe Howard
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dwe0608

ASKER

Thanks for the input MacroShadow - is there any need to destroy the object created?

Function ValidateEmail(ByVal email As String) As Boolean
    dim regEx as object
   set regEx = CreateObject("vbscript.regexp")
    With regEx
        .Global = True
        .IgnoreCase = True
        .Pattern = "^([a-zA-Z0-9_\-\.]+)@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$"
        ValidateEmail = .Test(email)
    End With
   set regEx = nothing
End Function

Open in new window

It's a good habit.
Avatar of dwe0608

ASKER

MacroShadow - can you explain what this pattern means - ie I understand the regular expression object etc, but cannot follow the pattern and what it means ...

.Pattern = "^([a-zA-Z0-9_\-\.]+)@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$"

Open in new window

Avatar of dwe0608

ASKER

Thanks for the help ... great answer ...
About "^([a-zA-Z0-9_\-\.]+)@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$"

^
It checks that the input starts

([a-zA-Z0-9_\-\.]+)
with any number of characters among a-z A-Z 0-9 _ - .

@
up to the first @

[a-z0-9-]+
followed by letters, digits and dashes,

(\.[a-z0-9-]+)*
followed optionally by a dot followed by letters, digits and dashes

(\.[a-z]{2,3})$
and ends with a dot followed by 2 or 3 letters.
Avatar of dwe0608

ASKER

what a great explanation ... thanks greatly ... theres really no need to enhance that pattern then is there ...

Thanks for all the support and information ...

Regards

DWE
It could be enhanced. There are a number of e-mail addresses which would be rejected.
For example, the following characters are allowed in the local part of the address ! # $ % & ' * + - / = ? ^ _ ` { | } ~

Also, a number of wrong addresses are accepted by that regular expression.
For example, the dot is not allowed at the start or end of the local part, and you cannot have two dots side-by-side.

The domain part should not start with a hyphen.

Here is a regex including the comments I made:
^([a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)(\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*@[a-z0-9][a-z0-9-]*(\.[a-z0-9-]+)*(\.[a-z]{2,3})$

Open in new window


There are other rules, like quoted text, comments and accented characters.
Every little rule you add will lengthen the expression significantly.
I don't usually bother with that, making the existing libraries check it themselves. I don't know if vbscript has one.