Solved

Conversion of Vb.net Function

Posted on 2014-09-14
11
224 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
Comment Utility
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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
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 26

Accepted Solution

by:
MacroShadow earned 500 total points
Comment Utility
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
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 26

Expert Comment

by:MacroShadow
Comment Utility
It's a good habit.
0
 
LVL 1

Author Comment

by:dwe0608
Comment Utility
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
Comment Utility
Thanks for the help ... great answer ...
0
 
LVL 11

Expert Comment

by:louisfr
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
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…

744 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

18 Experts available now in Live!

Get 1:1 Help Now