How to do Integer model validation in asp.net mvc 2

Luke101 picture Luke101 · Aug 23, 2010 · Viewed 44.9k times · Source

I have a registration form and the user must enter the square footage of their house. I would like this value to be only an integer. Is there a way to validate this value using attributes asp.net mvc?

Answer

Tom Glenn picture Tom Glenn · Sep 13, 2011

Realise this has already been answered, but Stefanvds' answer is uneccessarily complicated. Just use MVCs built in validation attributes:

[DisplayName("Square Feet")]
[Required(ErrorMessage = "Square Feet is Required")]
[Range(0, int.MaxValue, ErrorMessage = "Square Feet must be a positive number")]
public int SquareFeet { get; set; }