AX 2012: Validating decimal numbers using REGULAR EXPRESSIONS with a find method

In this blog we will show you how to use regular expressions in a find method just using some
code lines and Sub Strings. This is very useful when you need to search an email, a postal code
or any specific string
We will to validate an amount with a whole value with any length, a point and the decimal
value with two ciphers.

1) Create a method that receive a string and return a Boolean
private boolean ValidateAmount(str _amount)
{
}
2) Create a TextBuffer variable to save and work with the string received, and three
Boolean variables to control the correct writing of the number parts
private boolean ValidateAmount(str _amount)
{
TextBuffer txtAmount = new TextBuffer();
boolean decimals = false;
boolean wholes = false;
boolean point= false;
}
3) Regular expressions in X++ are different that regular expressions in other .NET
languages (like C#) so we need to find the way to use the AX tools and some valid regular
expressions like ^[0-9]+$

Where:
^ is the start of the string
[0-9]+ means that will contain numbers from 0 to 9 of any length
$ means end of the string

Note: I have tried some regular expressions with the decimal point included like ^[0-
9]+(\.[0-9]{1,2})?$ but it does not work for me.

So we need get two sets of numbers around the point to validate them, for that we will use
subStr
private boolean ValidateAmount(str _amount)
{
TextBuffer txtAmount = new TextBuffer();
boolean decimals = false;
boolean wholes = false;
boolean point= false;
;


txtAmount.setText(subStr(_amount,strLen(_amount),-2));
txtAmount.regularExpressions(true); // Enable regular expressions in txtAmount
if (txtAmount.find("^[0-9]+$")) // Regular expression inside the find method
{
decimals = true;
}
txtAmount.setText(subStr(_amount,1,strLen(_amount)-3));
txtAmount.regularExpressions(true); // Enable regular expressions in txtAmount
if (txtAmount.find("^[0-9]+$")) // Regular expression inside the find method
{
wholes = true;
}
if ((subStr(_amount,((strLen(_amount))-2),1)) == '.')
{
point = true;
}



4) Finally you need to compare the three Boolean values to return true if all was validated
correctly and to return false if there is any error
if (!wholes || !decimals || !point)
return false;
else
return true;
}


5) For testing purposes you can use a job and write the following code to print an error
message:
static void Job12(Args _args)
{

str _amount = "66*33";

TextBuffer txtAmount = new TextBuffer();
boolean decimals = false;
boolean wholes = false;
boolean point= false;
;

txtAmount.setText(subStr(_amount,strLen(_amount),-2));
txtAmount.regularExpressions(true); // Enable regular expressions in txtAmount
if (txtAmount.find("^[0-9]+$")) // Regular expression inside the find method
{
decimals = true;
}
txtAmount.setText(subStr(_amount,1,strLen(_amount)-3));
txtAmount.regularExpressions(true); // Enable regular expressions in txtAmount
if (txtAmount.find("^[0-9]+$")) // Regular expression inside the find method
{
wholes = true;
}
if ((subStr(_amount,((strLen(_amount))-2),1)) == '.')
{
point = true;
}
if (!wholes || !decimals || !point)
{
print 'error:' + _amount;
pause;
//return false;
}
else
{print 'good!' + _amount;
pause;}
//return true;
}

6) When you set _amount as 66*33 in the code and execute the job the following window is
deployed:

7) When you set _amount as 66514226.33 in the code:

We have shown you how to validate decimal numbers using regular expressions with a find
method in AX 2012.

Sign up to vote on this title
UsefulNot useful