validate the data entry of email addresses in D365 FO

 

Purpose:

The purpose of this post is to demonstrate how can we validate the data entry of email addresses.

Product:

Dynamics 365 for Finance and Operations

Description:

The code below uses regular expressions to validate the data entry of email addresses by hooking up a ValidatedField event handler to LogisticsElectronicAddress table.

Code:

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/// <summary>
///    This method accepts a email and validates this using REGEX
/// </summary>
/// <returns>
///    true or false based on Regex match
/// </returns>
static server boolean isValidEmail(Email _email)
{
    System.Text.RegularExpressions.Match match;
    System.Boolean netBool;
    boolean xppBool;
    str matchEmailPattern =
       @"^(([\w-]+\.)+[\w-]+|([a-zA-Z]{1}|[\w-]{2,}))@"
     + @"((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?
       [0-9]{1,2}|25[0-5]|2[0-4][0-9])\."
     + @"([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?
       [0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|"
     + @"([\w-]+\.)+[a-zA-Z]{2,4})$";
     
    new InteropPermission(InteropKind::ClrInterop).assert();
    match = System.Text.RegularExpressions.Regex::Match(_email, matchEmailPattern);
    netBool = match.get_Success();
    xppBool = netBool;
    CodeAccessPermission::revertAssert();
 
    return xppBool;
}
 
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
[DataEventHandler(tableStr(LogisticsElectronicAddress), DataEventType::ValidatedField)]
public static void LogisticsElectronicAddress_onValidatedField(Common sender, DataEventArgs e)
{
    LogisticsElectronicAddress logisticsElectronicAddress = sender;
    ValidateFieldEventArgs validateFieldEventArgs = e as ValidateFieldEventArgs;
    LogisticsElectronicAddressLocator locator;
    boolean ret = validateFieldEventArgs.parmValidateResult();
 
    if (ret)
    {
        switch (validateFieldEventArgs.parmFieldId())
        {
            case fieldNum(LogisticsElectronicAddress, Locator):
                 
                if (logisticsElectronicAddress.Type == LogisticsElectronicAddressMethodType::Email)
                {
                    locator = logisticsElectronicAddress.Locator;
 
                    if (!LogisticsElectronicAddress_SXSExtension::isValidEmail(locator))
                    {
                        ret = checkFailed("Email address is invalid.");
                    }
                }
 
                break;
 
            default:
                break;
        }
    }
 
    validateFieldEventArgs.parmValidateResult(ret);
}

Comments