我有一个不需要的字段,但我仍想向其添加一个正则表达式。
$.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" })
由于不需要该字段,因此无法正常工作。有什么想法有用吗?
使您的正则表达式为:\ s * |(您想要的正则表达式)。正如Tuskan360所说,其含义是“一个空字符串或与您的正则表达式匹配的字符串”
应该将其添加为答案而不是评论。
您可以将其用作正则表达式\ ^ $ |(您想要的正则表达式)。