Monday, April 22, 2024

Validate multiple emails using X++ , Regex

    ///
    /// </summary>
    /// <param name = "_fieldIdToCheck"></param>
    /// <returns></returns>
    public boolean validateField(FieldId _fieldIdToCheck)
    {
        boolean ret;
    
        ret = super(_fieldIdToCheck);

        switch (_fieldIdToCheck)
        {
            case fieldNum(Reporting, Email) :
              
                container email = str2con(this.Email, SysAppUtilities::semiColon);

                for (int i = 1; i <= conLen(email); i++)
                {
                    if (!Reporting::isValidEmail(conPeek(email, i)))
                    {
                        ret = checkFailed(strFmt("%1: %2", "@SYS334523", conPeek(email, i)));
                    }
                }

                break;
        }
    
        return ret;
    }

    /// <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;
    }

No comments:

Post a Comment

Table browser URL in D365FO

Critical Thinking icon icon by Icons8