web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Dynamics 365 Community / Blogs / DAX Beginners / How to: Validate special ch...

How to: Validate special characters

Christian Silva Profile Picture Christian Silva 707

Good day everyone,

This week I would like to share a class used to validate special characters.
I couldn’t find any propertie on table fields like “Block special characters”, because of that I am using a class that validate each character of a string using containers. Normally I call this class by Overriding the method ValidateField() from table.

This is how it works on ValidateField():

public boolean validateField(FieldId _fieldIdToCheck)
{
    boolean ret;

    ret = super(_fieldIdToCheck);

    if(ret)
    {
        switch(_fieldIdToCheck)
        {

            case fieldNum (LogisticsEletronicAddress, Locator):
                    ret = ValidateCharacter::validateCharacter(this.Locator) ;

                    if(!ret)
                        error('The eletronic address has invalid characters');
                    break;
        }
    }
    return ret;
}

For example, I have used the Table LogisticsEletronicAddress which is responsible to save contact information like e-mail and telephone. I will check if I am validating the field Locator and then call my class. Suppose I have typed “264-497=3131”.

This is my class:

public static boolean validateCharacter( str _name)
{
    System.Text.RegularExpressions. Match     regExMatch;
    boolean                                 isValid = true ;
    int                                     lenght;
    int                                     counter;
    container                               specialCharacters;
    str                                     value;
    ;
   
    // Here I insert all the special characters I want to validate.   
    specialCharacters = conIns (specialCharacters,  1, "/");
    specialCharacters = conIns (specialCharacters,  2, "[");
    specialCharacters = conIns (specialCharacters,  3, "]");
    specialCharacters = conIns (specialCharacters,  4, ";");
    specialCharacters = conIns (specialCharacters,  5, "=");
    specialCharacters = conIns (specialCharacters,  6, "\");
    specialCharacters = conIns (specialCharacters,  7, ":");
    specialCharacters = conIns (specialCharacters,  8, ",");
    specialCharacters = conIns (specialCharacters,  9, ".");
    specialCharacters = conIns (specialCharacters, 10, "!");
    specialCharacters = conIns (specialCharacters, 11, "-");
    specialCharacters = conIns (specialCharacters, 12, "(");
    specialCharacters = conIns (specialCharacters, 13, ")");
    specialCharacters = conIns (specialCharacters, 14, "*");
    specialCharacters = conIns (specialCharacters, 15, "&");
    specialCharacters = conIns (specialCharacters, 16, "^");
    specialCharacters = conIns (specialCharacters, 17, "#");
    specialCharacters = conIns (specialCharacters, 18, "@");
    specialCharacters = conIns (specialCharacters, 19, "_");
    specialCharacters = conIns (specialCharacters, 20, "$");
    specialCharacters = conIns (specialCharacters, 21, "%");
    specialCharacters = conIns (specialCharacters, 22, "?");
    specialCharacters = conIns (specialCharacters, 23, "+");
    specialCharacters = conIns (specialCharacters, 24, "{");
    specialCharacters = conIns (specialCharacters, 25, "}");
    specialCharacters = conIns (specialCharacters, 26, "");
    specialCharacters = conIns (specialCharacters, 28, "~");

    // I set my container lenght size.
    lenght = conLen (specialCharacters);

    // Looping through the container
    for (counter = 1 ; counter <= lenght; counter++)
    {
        // Getting the container value.
        value = conPeek (specialCharacters, counter);

        // Here I use the method StrContains(), it will check the String
        // character-by-character and return true if it matches the desired value.
        if (strContains(_name, value))
        {
            isValid = false ;
        }
    }
    // Return validation result.
    return isValid;
}

I have used all the special characters that I know of but feel free to add as much as your client requires :). This code will check the string "264-497=3131" character-by-character and match all the 28 special characters and return it as false (Let's forget that my telephone number accepts alphabetical characters haha).


This was originally posted here.

Comments

*This post is locked for comments