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:
/// <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);
}
*This post is locked for comments