Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics AX (Archived)
Answered

Validation for numeric values in AX

(0) ShareShare
ReportReport
Posted on by 11,627

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

  • Suggested answer
    MC08 Profile Picture
    MC08 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();

    }

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

    Hay,

    You can use the function isInteger(abcdx).

    Thanks

    Shahid

  • Suggested answer
    Martin Dráb Profile Picture
    Martin Dráb 230,354 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).

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

    Thanks Nitesh for fast response.

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

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

  • syed baber Profile Picture
    syed baber 11,627 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
    Community Member Microsoft Employee 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

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

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

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

Congratulations 2024 Spotlight Honorees!

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December!

Congratulations to our December super stars! 🥳

Get Started Blogging in the Community

Hosted or syndicated blogging is available! ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,622 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,354 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans