Use .Net to validate E-mail in NAV 2009 R2

9 Feb

Last week at the Dutch Dynamics Community Event, I showed an example of how to use .Net interoperability in Microsoft Dynamics NAV 2009 R2 to check if an E-mail address has the correct format.

Below is the complete code of this example:

Variables

NameDatatypeSubtypeLength
RegExDotNet‘System, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089’ .System.Text.RegularExpressions.Regex
RegExOptionsDotNet‘System, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089’ .System.Text.RegularExpressions.RegexOptions
PatternText80
ResultBoolean

Code

[codesyntax lang=”reg” container=”div”]

ValidateEmail(Email : Text[80]) : Boolean
IF NOT ISSERVICETIER THEN
  EXIT(TRUE);
Pattern := '^[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}$';
RegExOptions := 0;
RegExOptions := RegExOptions.Parse(RegExOptions.GetType(),'IgnoreCase');
Result := RegEx.IsMatch(Email, Pattern, RegExOptions);
EXIT(Result);

[/codesyntax]

One remark about the RegExOptions variable. As you can see the value is first set to 0. That is because of the next line of code where the RegExOptions.GetType() method is called. The method GetType() is not a static method and therefore it can’t be used with an uninstantiated object. Setting the value to 0 forces the RegExOptions variable to instantiate.

Of course it is possible to just write RegExOptions := 1; which is equivalent to IgnoreCase. But that is not very readible, isn’t it? Okay, you could put a comment behind the line, but for the purpose of the demo I just took the opportunity to also show how to handle .Net Enums in NAV 2009 R2.

Just put this code in a central place and call it from the OnValidate trigger of any E-mail field. Since only .Net Framework assemblies are used, there is no need for deploying dll’s to the Add-ins folder.

Oh, one last comment: this code is exclusively for use with RTC and / or NAV Server.

This was just one, simple example of how to use .Net Interoperability with Dynamics NAV 2009 R2. There is so much more available in the .Net Framework. Explore it and enjoy it!

11 thoughts on “Use .Net to validate E-mail in NAV 2009 R2

  1. Thanks for writing this post.
    When I try to add the Local Variables: RegEx and RegExOptions, I select DotNet as the datatype and then click the AssistEdit button of SubType.
    The .NET Type List appears and I click on the Lookup arrow of the Assembly field.
    The Assembly List has two tabs: DynamicsNAV (which is empty) and .NET (which as many records).

    I do not see System.Text.RegularExpressions.Regex or System.Text.RegularExpressions.RegexOptions.

    What am I missing?
    Thanks.

  2. Hi Ashok,

    Both classes can be found in the assembly System.dll. This assembly can be found on the .Net tab (the NAV tab only shows the .Net assemblies that are in the Add-ins folder of the client).

    After you select the System assembly, the System.Text.RegularExpressions classes can be found near the bottom of the .Net Type list.

    How could you know? At first, take a look at the variable specification in the post. The Subtype starts with ‘System’. That is the name of the assembly.

    If you want to know the assembly of a given class, you should look at the documentation. For the .Net Framework I recommend to use the MSDN Library. In case of the Regex class, you can find the documentation on this page: http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.aspx. Somewhere in the top of page the assembly name can be found.

    Success!
    Arend-Jan

  3. I was able to get the local variables setup successfully.

    What would I put in place of the following part of your code?
    ‘IgnoreCase’

    I looked at the documentation on the internet but didn’t really understand it.

    Thanks,
    Ashok.

  4. My last message didn’t come out too clearly. I was refering to the 2nd parameter of the RegexOptions.Parse function. Not sure what I need to put in here as if I use it exactly as shown, NAV compilation fails.

  5. Thanks to copy/paste the code wasn’t correct. The code should work now.
    The parameter ‘IgnoreCase’ is just a text, nothing else.

  6. Hello Arend Jan,

    I’m trying to add RegEx code to NAV 2013, but in the .NET list I see RegEx version 4.0.0.0 where your example has version 2.0.0.0
    Biggest problem I have is that I am not able to select it properly. When I got System.Text.RegularExpressions, I don’t see a list of subtypes. Do you know a work around?

    • Hi Arjan,

      In the Assembly List, don’t look for the System.Text.RegularExpressions. Instead, select the next assembly (use version 4 for NAV 2013):

      Name Version Culture Public Key Token Processor Architecture
      System 4.0.0.0 b77a5c561934e089 MSIL

      Now you have a Type List where you can find System.Text.RegularExpressions.Regex

      How could you know that?

      There are two ways:
      1. Look at the variable list in the post, it says ‘System’ before the version. That means that the System.dll assembly is used.
      2. Search on MSDN for the RegEx class (http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex(v=vs.100).aspx). It will tell you that it resides in the System.dll assembly.

      Kind regards,
      Arend-Jan Kauffmann

Leave a Reply to asava samuelCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.