最新消息:ww12345678 的部落格重装上线,希望大家继续支持。

验证电子邮件地址 / Validate email address

网络文摘 William 33浏览 0评论

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);
}
发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址