asp.net mvc 2 数据注释验证

发布于 2024-08-07 12:59:11 字数 316 浏览 2 评论 0原文

我刚刚尝试了新版本的框架(asp.net mvc 2 预览版 2),其中包括集成了 jQuery 的服务器和客户端验证,看起来不错。

它具有我的模型(实体)中属性的属性,例如 DataType。 我展开查看我有哪些选项,所以它拥有您想要的所有内容,例如 DurationPhoneNumber :)

但我没有像 Number 这样的简单数据类型

regEx 并不是一个好主意,因为我想使用 jQuery 数字验证器的内置选项

I have just tried new version of framework (asp.net mvc 2 preview 2) that includes server and client validation with jQuery integrated, looks fine.

it has attributes for properties in my model (entity) like DataType.
where i expand to see which options i have so it has all what u want u dont, like Duration and PhoneNumber :)

but i dont have simple DataType like Number!

regEx does not a good idea for it, cuz i want to user built-in option of jQuery validator for number

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

乄_柒ぐ汐 2024-08-14 12:59:11

范围验证器将强制执行数字。至少,xVal 与 DataAnnotations 和 jQuery.validate 的集成是这样工作的。我不知道这在 MVC 2 中是否同样有效,但值得一试。

[Range(0, Int32.MaxValue)]

Range validators will enforce numbers. At least, it works that way with xVal's integration with DataAnnotations and jQuery.validate. I don't know if this works the same way in MVC 2, but it's worth a try.

[Range(0, Int32.MaxValue)]
逐鹿 2024-08-14 12:59:11

使用整数属性怎么样:

public int Number { get; set; }

这将确保只能存储数字。

How about using an integer property:

public int Number { get; set; }

This will ensure that only numbers can be stored.

单身情人 2024-08-14 12:59:11

实际上,您很少需要任何数字(如 int 的 0 到 2147483647)。大多数时候指定一个范围是一个非常好的主意。

对于电话号码,创建您自己的属性来验证电话号码。只需创建一个类并继承 ValidationAttribute

Actually, you will very seldom want to have any number (as in 0 to 2147483647 for a int). Specifying a range is most of the time a very good idea.

As for phone numbers, create your own attribute validating the phone number. Simply create a class and inherit ValidationAttribute

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文