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

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)
Answered

Validation for numeric values in AX

(0) ShareShare
ReportReport
Posted on by 11,633

Hi All,

I am importing some data from csv file. I need to validate some columns where the values should be numeric. I need to check the value if it is not numeric, then I need to throw an error. Is there an easy way in X++ to do this? I am using the below method, but it is returning false in case of real values:

boolean isNumeric(str strToValidate)
{
    boolean ok = true;
    ;


    ok = (strlen(strToValidate)) == strlen(strkeep(strToValidate,'1234567890'));


    return ok;
}

Thanks,

Baber.

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Community Member Profile Picture
    on at
    RE: Validation for numeric values in AX

    it is because, a real number will have . (decimal) also in the string

  • Verified answer
    Community Member Profile Picture
    on at
    RE: Validation for numeric values in AX

    so your function will be something like this

    boolean isNumeric(str strToValidate)

    {

       boolean ok = true;

       ;

       ok = (strlen(strToValidate)) == strlen(strkeep(strToValidate,'1234567890.'));

       return ok;

    }

    please verify and update if this fix your issue

  • syed baber Profile Picture
    11,633 on at
    RE: Validation for numeric values in AX

    Yes I know this is the reason, so is there any other way to do this?

    Thanks,

    Baber.

  • Verified answer
    Community Member Profile Picture
    on at
    RE: Validation for numeric values in AX

    yes, you should try the modified function which I have posted

  • syed baber Profile Picture
    11,633 on at
    RE: Validation for numeric values in AX

    Thanks Nitesh for fast response.

  • Suggested answer
    Martin Dráb Profile Picture
    236,907 Most Valuable Professional on at
    RE: Validation for numeric values in AX

    Also don't forget that real values may be negative (character "-"), some countries use comma as the decimal mark and potentially there may be other things like a thousands separator.

    The implementation depends on what exactly you want to achieve (or which cases you decide to ignore). One possible alternative is using System.Single::TryParse(strToValidate, byref yourNumber).

  • Community Member Profile Picture
    on at
    RE: Validation for numeric values in AX

    Hay,

    You can use the function isInteger(abcdx).

    Thanks

    Shahid

  • Suggested answer
    MC08 Profile Picture
    170 on at
    RE: Validation for numeric values in AX

    I encounter similar requirement today and i tested following function can't handle if the value from string is like "1.1.1"

    boolean isNumeric(str strToValidate)

    {

       boolean ok = true;

       ;

       ok = (strlen(strToValidate)) == strlen(strkeep(strToValidate,'1234567890.'));

       return ok;

    }

    I used below function which using regular expression instead, it can handle the case mentioned above and also negative situation.

    public static boolean isDecimals(str _value)

    {

       System.Text.RegularExpressions.Match    myMatch;

       System.Text.RegularExpressions.Regex    regex;

       ;

       regex = new System.Text.RegularExpressions.Regex(@'^-?(([1-9][0-9]*)|(0))(?:\.[0-9]+)?$');

       myMatch = regex.Match(_value);

       return myMatch.get_Success();

    }

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Pallavi Phade – Community Spotlight

We are honored to recognize Pallavi Phade as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Tocauer Profile Picture

Martin Tocauer 4

#2
AlissonGodoy Profile Picture

AlissonGodoy 2

#2
Alexey Lekanov Profile Picture

Alexey Lekanov 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans