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

Announcements

News and Announcements icon
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
    240,196 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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the June Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans