Validating URL and Email Addresses with regexp in Lotus Notes

I needed to recreate this from scratch, and so I’m documenting this here for the next time.

Most of the credit should go to Julian Robichauxand his ls2j examples db, and in particultar the JakartaOroWrapper Library

My “invention” involves creating a isValidateURL and isValidateEmailAddress Regular Expression (or regexp ) routines that worked for me.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
Uselsx "*javacon"
Use "JakartaOroWrapper"

Function isValidateURL (Url As String) As Boolean
   
    Dim jSession As New JavaSession
    Dim oroClass As JavaClass
    Dim oro As JavaObject
    Dim vector As JavaObject
    Dim jError As JavaError
   
    '** get the ORO wrapper class and instantiate an instance of it
    Set oroClass = jSession.GetClass("JakartaOroWrapper")
    Set oro = oroClass.CreateObject
   
    Dim pattern As String  
'   pattern = "^((http[s]?|ftp):\/)?\/?([^:\/\s]+)((\/\w+)*\/)([\w\-\.]+[^#?\s]+)(.*)?(#[\w\-]+)?$"
'   pattern = "(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?"  
    pattern = "^(http|https|ftp)\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\-\._\?\,\'/\\\+&%\$#\=~])*$"
   
    isValidateURL = oro.matches(Url, pattern, False)   
End Function

Function isValidateEmailAddress (emailAddress As String) As Boolean
   
    Dim jSession As New JavaSession
    Dim oroClass As JavaClass
    Dim oro As JavaObject
    Dim vector As JavaObject
    Dim jError As JavaError
   
    '** get the ORO wrapper class and instantiate an instance of it
    Set oroClass = jSession.GetClass("JakartaOroWrapper")
    Set oro = oroClass.CreateObject
   
    '** here are the strings and patterns we'll play with
    Dim testString As String
    Dim pattern As String  
    'pattern = "\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b"
    'RFC_2822 version
    pattern = "[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?"
   
   
    isValidateEmailAddress = oro.matches(emailAddress, pattern, False)
   
   
End Function

How I used it was not very exciting but for completeness.

1
2
3
4
5
6
If Not NotesDoc.url(0) =  "" Then  
        If Not isValidateURL ( NotesDoc.url(0)) Then
            Msgbox "The URL Address ("+NotesDoc.url(0)+") doesn't look right, Please confirm it is correct.", 0 + 32, "Validation Error"       
            NotesUIDoc.GotoField("URL")
    End If
End If

A big cautionary note about the regexp patterns I used. for both Url’s and emails addresses, I seen them very short and very very long. It’s easy to make assumtion about the validate TLD and other aspects of the strings, so test test test, and be aware.

And, of course, this is but a small piece of what you could do with regexp!!

2010 Update

cross posted from Quick links as of June 4th 2010 is a an interesting, and non trival test of Comparing E-mail Address Validating Regular Expressions using PHP’s ereg() and preg_match() function finds a winner (where it’s better to accept a few invalid addresses than reject any valid ones) :

/^([\w\!\#$\%\&\’\*\+\-\/\=\?\^\`{\|\}\~]+\.)*[\w\!\#$\%\&\’\*\+\-\/\=\?\^\`{\|\}\~]+@((((([a-z0-9]{1}[a-z0-9\-]{0,62}[a-z0-9]{1})|[a-z])\.)+[a-z]{2,6})|(\d{1,3}\.){3}\d{1,3}(\:\d{1,5})?)$/i

I wonder if these results in torturing regexp email address recognizers will hold up given the variations in regular_expression engines?

I haven’t tested these in Notes Domino. can anyone verify that they work?

Leave a Reply