Solved

Conversion of Vb.net Function

Posted on 2014-09-14
11
244 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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
 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

688 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