?
Solved

Conversion of Vb.net Function

Posted on 2014-09-14
11
Medium Priority
?
249 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 27

Accepted Solution

by:
MacroShadow earned 2000 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
 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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.
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

764 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