MVC 4 - DataAnnotations - 验证的类型 [英] MVC 4 - DataAnnotations - Validation for Type

查看:78
本文介绍了MVC 4 - DataAnnotations - 验证的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code工作

  [必需(的ErrorMessage =价格是必需的。)
    [范围(typeof运算(十进制),1,9999,的ErrorMessage =价格XX.XX)]
    公共小数? productSku,则{搞定;组; }

在该页面与提交
价格=空字段错误消息价格是必需的。
价格=超过9999的错误消息是价格XX.XX。

然而,当我输入'AAAA'的错误信息
这个领域productSku,则必须是一个数字。

我如何更改消息,如果类型不正确?
像:价格必须是1-9999之间的小数/数字

----更新:----
下面code。与合作

NULL,不是小数,范围之间,而与.1的工作。

  [必需(的ErrorMessage =价格是必需的。)
    [RegularEx pression(@[0-9] * \\。?[0-9] +的ErrorMessage =价格必须是只是一个数字。)
    [范围(typeof运算(十进制),1,9999,的ErrorMessage =价格必须是之间的小数/数字{1}和{2}。)
    公共小数? productSku,则{搞定;组; }


解决方案

您可以用常规的前pression尝试:

  [RegularEx pression(@[0-9] * \\。?[0-9] +的ErrorMessage ={0}必须是数字。 )]

您也可以尝试在数据注释扩展:
<一href=\"http://dataannotationsextensions.org/Home/Wiki\">http://dataannotationsextensions.org/Home/Wiki

或写自己的实现,这样的事情:
<一href=\"https://github.com/srkirkland/DataAnnotationsExtensions/blob/master/DataAnnotationsExtensions/DigitsAttribute.cs\">https://github.com/srkirkland/DataAnnotationsExtensions/blob/master/DataAnnotationsExtensions/DigitsAttribute.cs

更新
与正则表达式(匹配9,999.99 $ | $ 0.70 | 0.1)

<$p$p><$c$c>[RegularEx$p$pssion(@\"^\\$?([1-9]{1}[0-9]{0,2}(\\,[0-9]{3})*(\\.[0-9]{0,2})?|[1-9]{1}[0-9]{0,}(\\.[0-9]{0,2})?|0(\\.[0-9]{0,2})?|(\\.[0-9]{1,2})?)$\",的ErrorMessage ={0}必须是数字。)

或使用范围有轻微的修改建议@马丁(实际上是一个更好的解决方案):

  [范围(typeof运算(十进制),0,9999,的ErrorMessage ={0}必须之间的小数/数字{1}和{2}。 )]

I have the following code working

    [Required(ErrorMessage = "Price is required.")]
    [Range(typeof(Decimal), "1", "9999", ErrorMessage = "Price xx.xx")]
    public decimal? productPrice { get; set; }

When the page is submitted with Price = EMPTY Field error message is "Price is required.". Price = over 9999 error message is "Price xx.xx".

However, when I type 'aaaa' the error message is "The field productPrice must be a number."

How can I change the message if type in not correct? Like : "Price must be a decimal/number between 1-9999.

---- UPDATE: ---- The below code worked with

NULL, Not Decimal, Between Range, BUT not working with ".1".

    [Required(ErrorMessage = "Price is required.")]
    [RegularExpression(@"[0-9]*\.?[0-9]+", ErrorMessage = "Price must be a Numbers only.")]
    [Range(typeof(Decimal), "1", "9999", ErrorMessage = "Price must be a decimal/number between {1} and {2}.")]
    public decimal? productPrice { get; set; }

解决方案

You can try with the regular expression:

[RegularExpression(@"[0-9]*\.?[0-9]+", ErrorMessage = "{0} must be a Number.")]

you can also try the Data Annotations Extensions: http://dataannotationsextensions.org/Home/Wiki

Or write your own implementation,something like this : https://github.com/srkirkland/DataAnnotationsExtensions/blob/master/DataAnnotationsExtensions/DigitsAttribute.cs

UPDATE With REGEX (Matches $9,999.99 | $0.70 | .1)

[RegularExpression(@"^\$?([1-9]{1}[0-9]{0,2}(\,[0-9]{3})*(\.[0-9]{0,2})?|[1-9]{1}[0-9]{0,}(\.[0-9]{0,2})?|0(\.[0-9]{0,2})?|(\.[0-9]{1,2})?)$", ErrorMessage = "{0} must be a Number.")]

Or using Range with a slight modification to @Martin suggestion (actually is a better solution):

[Range(typeof(Decimal), "0", "9999", ErrorMessage = "{0} must be a decimal/number between {1} and {2}.")]

这篇关于MVC 4 - DataAnnotations - 验证的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆