Axapta 验证类

发布于 2024-08-10 23:45:27 字数 1774 浏览 2 评论 0原文

我在 AX2009 中编写了一种处理正则表达式验证的方法。问题是,无论表达式或输入字符串是什么,它总是返回 false。不返回任何错误,只是“错误” 介意看一下吗?我可能错过了一些简单的事情。

这篇文章已更新,包含更正的方法,没有错误,因此您可以剪切并粘贴代码以在您的项目中使用。符合 BP 标准并随时可用。 - 尽情享受

static boolean validateMe(str regexFilter, str _testString)
{
    System.Text.RegularExpressions.Match regExMatch;
    boolean retVal;
    str regExpression;
    ;

    //See if any of the static expressions were selected
    switch (regexFilter)
    {
        case 'integer' :
            regExpression = '^\\d+$';
            break;
        case 'rgbcolor' :
            regExpression = '^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\,([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\,([01]?\\d\\d?|2[0-4]\\d|25[0-5])$';
            break;
        case 'number' :
            regExpression = '^(\\d+\\.?\\d*|\\d*\\.?\\d+)$';
            break;
        case 'email' :
            regExpression = '^([\\w-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([\\w-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$';
            break;
        case 'phone' :
            regExpression = '^(\\()?(\\d{3})(\\)|-)?([0-9]{3})(-)?([0-9]{4}|[0-9]{4})$';
            break;
        case 'nopunctationphone' :
            regExpression = '^\\d{10}$';
            break;
        default :
            //No static expression matched, use the passed-in value
            regExpression = regexFilter;
    }

    //see if the string matches
    if (_testString != '')
    {
        //see if string matches expression; validation is good
        regExMatch = System.Text.RegularExpressions.Regex::Match(_testString, regExpression);
        retVal = regExMatch.get_Success();
    }
    else
    {
        //string does NOT match expression; validation fails
        retVal = false;
    }

    return retVal;
}

I've written a method to handle regex validation in AX2009. Problem is that it always returns false, no matter what the expression or input string. Returns no errors, just 'false' Mind taking a look? I'm probably missing something simple.

This post has been updated to included the corrected method, without the error, so you can cut and paste the code for use in your project. BP compliant and ready for use. - Enjoy

static boolean validateMe(str regexFilter, str _testString)
{
    System.Text.RegularExpressions.Match regExMatch;
    boolean retVal;
    str regExpression;
    ;

    //See if any of the static expressions were selected
    switch (regexFilter)
    {
        case 'integer' :
            regExpression = '^\\d+
;
            break;
        case 'rgbcolor' :
            regExpression = '^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\,([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\,([01]?\\d\\d?|2[0-4]\\d|25[0-5])
;
            break;
        case 'number' :
            regExpression = '^(\\d+\\.?\\d*|\\d*\\.?\\d+)
;
            break;
        case 'email' :
            regExpression = '^([\\w-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([\\w-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)
;
            break;
        case 'phone' :
            regExpression = '^(\\()?(\\d{3})(\\)|-)?([0-9]{3})(-)?([0-9]{4}|[0-9]{4})
;
            break;
        case 'nopunctationphone' :
            regExpression = '^\\d{10}
;
            break;
        default :
            //No static expression matched, use the passed-in value
            regExpression = regexFilter;
    }

    //see if the string matches
    if (_testString != '')
    {
        //see if string matches expression; validation is good
        regExMatch = System.Text.RegularExpressions.Regex::Match(_testString, regExpression);
        retVal = regExMatch.get_Success();
    }
    else
    {
        //string does NOT match expression; validation fails
        retVal = false;
    }

    return retVal;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

◇流星雨 2024-08-17 23:45:27

您已经交换了它应该是的变量:

regEx = new System.Text.RegularExpressions.Regex(regExpression);

You have swapped the variables it should be:

regEx = new System.Text.RegularExpressions.Regex(regExpression);
战皆罪 2024-08-17 23:45:27

难道你需要转义字符串内的反斜杠吗?

regExpression = '^\\d*

ETC。

;

ETC。

Could it be that you need to escape the backslashes inside strings?

regExpression = '^\\d*

etc.

;

etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文