0

我需要匹配多个 1-5 位数字或空字符串以进行模型验证:
模型:

[RegularExpression(@"\d{0,5}", ErrorMessage = "Error")]
public string ServiceNumber { get; set; }

看法:

@Html.TextBoxFor(m => m.ServiceNumber)

这不会让我把输入留空。

4

2 回答 2

1

Have you tried \d{1,5}|^$? The ^$ is an empty string because ^ is the start of the string and $ is the end of the string in regex so ^$ means start and end string with nothing inside. The | means OR, either match 1-5 digits OR empty string.

于 2012-04-24T01:15:16.380 回答
1

Have you tried @"(\d{1,5})?" ?

于 2012-04-24T01:15:45.393 回答