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 :
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

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

  • Verified answer
    Community Member Profile Picture
    on at

    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

    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

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

  • syed baber Profile Picture
    11,633 on at

    Thanks Nitesh for fast response.

  • Suggested answer
    Martin Dráb Profile Picture
    237,801 Most Valuable Professional on at

    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

    Hay,

    You can use the function isInteger(abcdx).

    Thanks

    Shahid

  • Suggested answer
    MC08 Profile Picture
    170 on at

    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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans