DataAnnotation 验证属性的 Int 或 Number 数据类型
在我的 MVC3 项目中,我存储了 football/soccer/hockey/
... 体育比赛的得分预测。因此,我的预测类的属性之一如下所示:
[Range(0, 15, ErrorMessage = "Can only be between 0 .. 15")]
[StringLength(2, ErrorMessage = "Max 2 digits")]
[Remote("PredictionOK", "Predict", ErrorMessage = "Prediction can only be a number in range 0 .. 15")]
public int? HomeTeamPrediction { get; set; }
现在,我还需要更改数据类型的错误消息,在我的例子中为 int
。使用了一些默认值 - “HomeTeamPrediction 字段必须是一个数字。”。需要找到一种方法来更改此错误消息。该验证消息似乎也对远程验证进行了预测。
我尝试过 [DataType] 属性,但这似乎不是 system.componentmodel.dataannotations.datatype 枚举中的普通数字。
On my MVC3 project, I store score prediction for football/soccer/hockey/
... sport game. So one of properties of my prediction class looks like this:
[Range(0, 15, ErrorMessage = "Can only be between 0 .. 15")]
[StringLength(2, ErrorMessage = "Max 2 digits")]
[Remote("PredictionOK", "Predict", ErrorMessage = "Prediction can only be a number in range 0 .. 15")]
public int? HomeTeamPrediction { get; set; }
Now, I need also change error message for a data type, int
in my case. There is some default one used - "The field HomeTeamPrediction must be a number.". Need to find a way how to change this error message. This validation message also seem to take prediction for Remote validation one.
I've tried [DataType]
attribute but this does not seem to be plain number in system.componentmodel.dataannotations.datatype
enumeration.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
对于任何数字验证,您必须根据您的要求使用不同的不同范围验证:
对于整数
,对于浮点
,对于双精度
For any number validation you have to use different a different range validation as per your requirements:
For Integer
for float
for double
尝试以下正则表达式之一:
希望有帮助:D
Try one of these regular expressions:
hope it helps :D
在数据注释中使用正则表达式
Use regex in data annotation
尝试这个属性:
并且您还必须在验证器插件中注册该属性:
Try this attribute :
And also you must register the attribute in the validator plugin:
通过将属性设置为视图模型中的字符串,我能够绕过所有框架消息。
然后我需要在 get 方法
和 post 方法中进行一些转换:
这在使用 range 属性时效果最好,否则需要一些额外的验证来确保该值是数字。
您还可以通过将范围内的数字更改为正确的类型来指定数字的类型:
I was able to bypass all the framework messages by making the property a string in my view model.
Then I need to do some conversion in my get method:
and post method:
This works best when using the range attribute, otherwise some additional validation would be needed to make sure the value is a number.
You can also specify the type of number by changing the numbers in the range to the correct type:
您可以编写自定义验证属性:
然后您可以在您的属性上使用以下注释:
You can write a custom validation attribute:
On your property you can then use the following annotation:
近十年过去了,但该问题在 Asp.Net Core 2.2 中仍然存在。
我通过将
data-val-number
添加到输入字段来管理它,并在消息上使用本地化:almost a decade passed but the issue still valid with Asp.Net Core 2.2 as well.
I managed it by adding
data-val-number
to the input field the use localization on the message:ASP.NET Core 3.1
这是我对该功能的实现,它在服务器端工作,并且与 jquery 验证一起使用,就像任何其他属性一样,带有自定义错误消息,不引人注意:
属性:< /strong>
客户端逻辑:
最后是用法:
ASP.NET Core 3.1
This is my implementation of the feature, it works on server side as well as with jquery validation unobtrusive with a custom error message just like any other attribute:
The attribute:
Client side logic:
And finally the Usage:
另一个简单的解决方案将插入内置框架,包括通过扩展 System.ComponentModel.DataAnnotations.RegularExpressionAttribute 进行客户端验证。
使用几个常量作为允许 0+ 的正则表达式,或者使用 bool 标志允许 1+。
然后在模型上,您只需添加属性(确保您有可用于您创建的属性的命名空间:
或允许零
Another simple solution that will plug into the built in framework, including client side validation by extending the System.ComponentModel.DataAnnotations.RegularExpressionAttribute.
Use a couple of constansts for the regular expressions for allowing 0+, or allow 1+ with a bool flag.
And then on the model you just add the attribute(make sure you have the namespace availiable for the attribute you create:
or to allow Zero