我需要匹配多个 1-5 位数字或空字符串以进行模型验证:
模型:
[RegularExpression(@"\d{0,5}", ErrorMessage = "Error")]
public string ServiceNumber { get; set; }
看法:
@Html.TextBoxFor(m => m.ServiceNumber)
这不会让我把输入留空。
我需要匹配多个 1-5 位数字或空字符串以进行模型验证:
模型:
[RegularExpression(@"\d{0,5}", ErrorMessage = "Error")]
public string ServiceNumber { get; set; }
看法:
@Html.TextBoxFor(m => m.ServiceNumber)
这不会让我把输入留空。
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.
Have you tried @"(\d{1,5})?"
?