I created a sample Model for the student in Creating a basic ASP.NET MVC Application-Part I. There I have created a simple Model where it defines the data types that each variable or the field can have. Simply, the only restriction of the DateOfBirth variable is that it need to in type of DateTime. But there are many more things to validate in that field. For example, a user can give upcoming date as the Date of Birth. How these things can be validated? Models in ASP.NET MVC 3 provides the greater capabilities for that. Let’s discuss them in detail here.
First of all, add the following using statements in the model.
using System.ComponentModel.DataAnnotations; using System.Web.Mvc;
By default, the View displays the name of the Field as the Field name in a form table or whatever the display mode. For example the First Name of the student will be displayed as FirstName (no space in between)since we have not declared what should be displayed. We do need to display it as First Name. What we have to do is simply adding the [Display].
[Display(Name="First Name")]
public string FirstName { get; set; }
As it says, this is for validating the required fields. Basically a form or an entity may contain things that are must to have. For example, a person who create an account for a log-in must have a Username and Password. In our student example, the student must have almost all the fields. Will we find student without a Date of Birth, Address? No. So all fields are required fields.
Required field validation can be easily done by adding [Required] before the variable declaration.
[Required(ErrorMessage="First Name cannot be empty.")]
[Display(Name="First Name")]
public string FirstName{ get; set; }
Here I have added a custom error message as well. So in a case that First Name is unavailable, this error message will prompt. if the ErrorMessage was not there(when we have only [Required]), then the default error message of MVC will be displayed.
It provides the capability of making formatted content to be made available. For example, a password must be represented in special characters. It can be simply done by setting the Data Type for Password. Then it will create a PasswordFor control over TextBoxFor for editing when you generating the view.
[DataType(DataType.Password)]
public string Password { get; set; }
Likewise, you can have data types such as Date,DateTime, Currency,Email Address and etc…
There may be a particular length range that you might need to have in a filed. For example, you may need the password to be minimum 6 characters in length and maximum of 20 characters. It can be simply defined like this.
[MinLength(6,ErrorMessage="Password must contain more than 5 characters.")]
[MaxLength(20, ErrorMessage = "Password must be less than 20 characters.")]
public string Password { get; set; }
When creating an account, you may ask the user to Retype the password. In such case, it must be same as the first Password. It can be simply done through [Compare].
[Display(Name = "Password")]
public string PWord { get; set; }
[Display(Name = "Confirm Password")]
[Compare("PWord",ErrorMessage="Passwords do not match")]
public string CPWord { get; set; }
Here are the most commonly used validations that I have seen. There are many more that would be nice to tryout such as [Association],[AllowHtml],[Range] and etc…
Just try them out. Feel free to leave a comment if any questions are there.