Solved

Conversion of Vb.net Function

Posted on 2014-09-14
11
228 Views
Last Modified: 2014-09-17
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
0
Comment
Question by:dwe0608
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 11

Expert Comment

by:louisfr
ID: 40322529
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

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40322563
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  ; )
0
 
LVL 11

Expert Comment

by:louisfr
ID: 40322580
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.
0
 
LVL 27

Accepted Solution

by:
MacroShadow earned 500 total points
ID: 40322764
As mentioned by louisfr the validating pattern isn't very accurate. Here is a small regex email validation function.
Function ValidateEmail(ByVal email As String) As Boolean
    With CreateObject("vbscript.regexp")
        .Global = True
        .IgnoreCase = True
        .Pattern = "^([a-zA-Z0-9_\-\.]+)@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$"
        ValidateEmail = .Test(email)
    End With
End Function

Open in new window

Just a word of caution, validating emails is very very complex, surely the pattern I wrote won't cover all valid or non-valid cases, but it should cover most common email addresses.
0
 
LVL 1

Author Comment

by:dwe0608
ID: 40324759
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

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 27

Expert Comment

by:MacroShadow
ID: 40324775
It's a good habit.
0
 
LVL 1

Author Comment

by:dwe0608
ID: 40324845
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

0
 
LVL 1

Author Closing Comment

by:dwe0608
ID: 40324847
Thanks for the help ... great answer ...
0
 
LVL 11

Expert Comment

by:louisfr
ID: 40327408
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.
0
 
LVL 1

Author Comment

by:dwe0608
ID: 40327448
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
0
 
LVL 11

Expert Comment

by:louisfr
ID: 40327512
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.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now