Friday, October 20, 2017

Validate Email Address in NAV using RegEx

business-man-1002781_1920

Last week I was working on a shipment notification project where I need to send Email Notification of the shipment and one thing we need to check is the email address is valid or not. In Standard NAV the SMTP mail codeunit or Mail Management codeunit has a function to checkValidEmailAddress but it does a very basic validation and it did not meet our needs so I have written a new function to validate the email address.

I have used  RegEx( Regular Expression) and According to Wikipedia

“A regular expression, regex or regexp[1] (sometimes called a rational expression)[2][3] is, in theoretical computer science and formal language theory, a sequence of characters that define a search pattern. Usually this pattern is then used by string searching algorithms for "find" or "find and replace" operations on strings.”

Since we have the access the functions of RegEx function using DotNet, I went a ahead and wrote the following function to validate the email address using RegEx.

In our case we could store multiple email addresses in a field, so I have used String Array to parse and validate the email address.

PROCEDURE ValidateEmailAddresses@1000000008(EmailAddresses@1000000000 : Text);
     VAR
       RegEx@1000000004 : DotNet "'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Text.RegularExpressions.Regex";
       DotNetString@1000000003 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.String";
       EmailAddrArray@1000000002 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Array";
       Convert@1000000001 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Convert";
       I@1000000005 : Integer;
       EmailAddress@1000000006 : Text;
     BEGIN
       EmailAddresses := CONVERTSTR(EmailAddresses,',',';');
       EmailAddresses := DELCHR(EmailAddresses,'<>');
       EmailAddrArray := RegEx.Split(EmailAddresses,';');
       FOR I := 1 TO EmailAddrArray.GetLength(0) DO BEGIN
         EmailAddress := EmailAddrArray.GetValue(I-1);
         IF NOT RegEx.IsMatch
               (EmailAddress,'^[\w!#$%&*+\-/=?\^_`{|}~]+(\.[\w!#$%&*+\-/=?\^_`{|}~]+)*@((([\-\w]+\.)+[a-zA-Z]{2,4})|(([0-9]{1,3}\.){3}[0-9]{1,3}))$') THEN
           ERROR(Text106);
       END;
     END;
I got the above email address regular expression pattern from the below link, so please visit the below to know what validation it does.
https://www.rhyous.com/2010/06/15/regular-expressions-in-cincluding-a-new-comprehensive-email-pattern/

If you have any other tips or suggestions , please do share them in the comments below.


Share:

1 comments:

jhon j. bret said...

Awesome, i was searching for the same issue, and just landed to your blog. Weldone dude. I am also a blogger, and learning the art of pro resume writer. Keep up the good work.