Range DataAnnotation attribute in MVC3 Razor

  1. The Range attribute specified the minimum and maximum constraints for a numerical value.
  2. We can use this attribute to accepts the age of particular age group users.

Example 1 :

In the above example, we specified the minimum and the maximum parameter for the age field.
When this range is not adhered, validation message is shown.
Here we have not specified the Error Message. It will pick the default message.

UI :

On UI we supplied age as 19, which is one less than the minimum value of the range. The validation fails for the field and validation message is shown.

Example 2 :

In above example along with the range we have also specified the error message to display on UI.

UI :

Example 3 :

We can also pick the error message from resource file. The above code expects a resource file with name Multi.resx having a key Age_Validation which holds the validation message. When validation fails system picks the validation message from the resource file specified.

Resource :

UI :

Watch Video :


Post a Comment