开发者

asp.net mvc 2 DataAnnotations validation

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


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)]


How about using an integer property:

public int Number { get; set; }

This will ensure that only numbers can be stored.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜