asp.net-mvc Range Attribute


Example

The Range attribute can decorate any properties or public fields and specifies a range that a numerical field must fall between to be considered valid.

[Range(minimumValue, maximumValue)]
public int Property { get; set; }

Additionally, it accepts an optional ErrorMessage property that can be used to set the message received by the user when invalid data is entered :

[Range(minimumValue, maximumValue, ErrorMessage = "{your-error-message}")]
public int Property { get; set; }

Example

[Range(1,100, ErrorMessage = "Ranking must be between 1 and 100.")]
public int Ranking { get; set; }