非必填字段上的正则表达式

时间:2011-08-20 19:30:24

标签: c# regex asp.net-mvc-3

我有一个不需要的字段,但我仍想为它添加正则表达式。

$.validator.addMethod(
            "regex",
            function (value, element, regexp) {
                var check = false;
                var re = new RegExp(regexp);
                return this.optional(element) || re.test(value);
            },
            "Please check your input."
        );


        $(".numeric").rules("add", { number: true });

    });

@Html.TextBoxFor(model => model.PurchaseAmount, new { @class = "currency numeric", Value = String.Format("{0:0.00}", Model.PurchaseAmount), style = "width:160px" })

因为不需要该字段,所以不会工作。任何有用的想法?

2 个答案:

答案 0 :(得分:1)

制作正则表达式:be \ s * |(你想要的正则表达式)。正如Tuskan360所说,这意味着“空字符串或与正则表达式匹配的字符串”

应该将此作为答案而不是评论添加。

答案 1 :(得分:0)

您可以将其用作正则表达式 \ ^ $ |(您想要的正则表达式)