我正在使用数据注释验证,并尝试在字段与我的正则表达式不匹配时生成自定义错误消息。这是我的模型:
[DisplayName("EIN / SSN")]
[StringLength(4, MinimumLength = 4, ErrorMessage = "Please enter last 4 digits of EIN or SSN.")]
[RegularExpression("^\\d{1,5}$", ErrorMessage = "Please enter numeric characters.")] // 5 so it'll let the above message fire instead
[Required(ErrorMessage = "Please enter last 4 digits of EIN or SSN Number.")]
public string EIN { get; set; }
[DisplayName("Issued amount")]
[RegularExpression("^-?\\d{1,13}(?:\\.\\d{1,2})?$", ErrorMessage = "Please enter numeric characters.")]
[Required(ErrorMessage = "Please enter exact dollar amount as it appears on your bank statement.")]
//[DataType(DataType.Currency)]
public decimal IssueAmount { get; set; }
注意:EIN 字段的验证工作正常,但是当我为 IssueAmount 输入任何 alpha 时,我仍然收到内置错误:“值 'fgyjfgyj' 对于已发行金额无效。” 而不是“请输入数字字符”。我已经通过验证器运行了我的正则表达式,它似乎在我想要的时候匹配。
有没有人有任何想法?
编辑: 查看:
@Html.LabelFor(model => model.EIN)<em>Last 4 only</em>
@Html.TextBoxFor(model => model.EIN, new {maxlength = 4})
@Html.ValidationMessageFor(model => model.EIN)
@Html.LabelFor(model => model.IssueAmount)<em>Must be exact amount</em>
@Html.TextBoxFor(model => model.IssueAmount, new { maxlength = 16 })
@Html.ValidationMessageFor(model => model.IssueAmount)
我的控制器中的代码是否应该影响错误?