How to Validate special characters in D365FO

 Purpose:

The purpose of this document is to demonstrate how we can we validate special characters in given values in field  X++ based on a business logic.

Product:

Dynamics 365 for Finance and Operations.

Development approach:

Customization through CODE, we need to  validate special characters in given values in field

Development:

Create a new method validateCharacter() which help us to store all special characters into container and when users given inputs system will check all characters in given inputs as per below code.

added validate field to validate user inputs

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



This is my class:


public static boolean validateCharacter(str _name)
{
System.Text.RegularExpressions.Match regExMatch;
boolean isValid = true;
int length;
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,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,27, ">");
specialCharacters = conIns(specialCharacters,29, "~");
// I set my container length size.
length = 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;
}

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 “test&8++==$#%″.

Comments