【问题标题】:Convert c# regex to javascript将 c# 正则表达式转换为 javascript
【发布时间】:2014-11-05 22:26:46
【问题描述】:

我有一个字段,我针对三个不同的用户输入验证对其进行验证。我已经在服务器端 C# 上完成了它,但我也需要在 Jquery/Javascript 中完成它,因为这些文本框位于页面上的对话框中:

if (part.RevisionNumber != null)
            {
                bool isPartRevNA = false;

                //Check if Revision Number = NA or N\A
                if(part.RevisionNumber == "NA" || part.RevisionNumber == @"N\A")
                    isPartRevNA = true;

                if (part.RevisionNumber.Length > 4)
                {
                    ModelState.AddModelError(string.Empty, "Revision# is an optional field but the format must be :3-digit, numeric values only (e.g., ‘102’) or 2. 4-digit, 3 numeric values plus 1 alpha (e.g., ‘102B’) or 3. Character string to represent not applicable (e.g., ‘NA’, or ‘N/A’). ");
                    isPartRevNA = false;
                }
                if (!isPartRevNA)
                {
                    //revision number not na or n\a
                    //1. check next regex condition of if 3 digit numeric value
                    if (part.RevisionNumber.Length == 3)
                    {
                        string pattern = @"\A(\d){3}\Z";
                        Regex rgx = new Regex(pattern);

                        bool match = rgx.IsMatch(part.RevisionNumber);
                        if (!match)
                            ModelState.AddModelError(string.Empty, "Revision# is an optional field but the format must be :3-digit, numeric values only (e.g., ‘102’) or 2. 4-digit, 3 numeric values plus 1 alpha (e.g., ‘102B’) or 3. Character string to represent not applicable (e.g., ‘NA’, or ‘N/A’). ");
                    }

                    //2. check regex condition if length 4 
                    if (part.RevisionNumber.Length == 4)
                    {
                        string pattern = @"\d{3}[A-Za-z]";
                        Regex rgx = new Regex(pattern);

                        bool match = rgx.IsMatch(part.RevisionNumber);
                        if (!match)
                            ModelState.AddModelError(string.Empty, "Revision# is an optional field but the format must be :3-digit, numeric values only (e.g., ‘102’) or 2. 4-digit, 3 numeric values plus 1 alpha (e.g., ‘102B’) or 3. Character string to represent not applicable (e.g., ‘NA’, or ‘N/A’). ");
                    }
                }
            }

该字段是可选的,但在以下事件中,我将在 textarea 聚焦事件中捕获用户输入,如果它有一个值并且不符合用户条件,那么我将重置该值并提醒用户正确输入应该他们选择:

1) 可能是 NA 或 N\A 2) 3 位,仅限数值(例如,‘102’),@"\A(\d){3}\Z"; 3) 4 位,3 个数值加上 1 个字母(例如,‘102B’)@"\d{3}[A-Za-z]";

$(".foo").focusout( function() {


                alert( $(this).val() );
        }).click(function (e) {
            e.stopPropagation();
            return true;
        });

【问题讨论】:

    标签: javascript c# jquery


    【解决方案1】:

    以下正则表达式应该自己完成这项工作,并且可以在 .NET 和 JS 中工作:

    ^(?:N\\?A|\d{3}[A-Za-z]?)$
    

    很简单:

    • 匹配NA,在两者之间可选\
    • 或 3 位数字后跟可选字母。

    【讨论】:

      猜你喜欢
      • 2011-10-02
      • 1970-01-01
      • 2023-03-09
      • 2023-04-10
      • 1970-01-01
      • 2016-09-13
      • 1970-01-01
      • 2019-05-11
      相关资源
      最近更新 更多