Validation in MVC4 Razor with the Data Annotation Validators in “MODEL”

Data Annotation

Data validation play major role when developing web application. In Asp.net MVC, we can easily implement validation to application by using Data Annotation attribute to model. Data Annotation attribute classes are present in System.ComponentModel. DataAnnotations namespace. It is a common validation attributes which extend the built-in ASP.NET DataAnnotations (Required, Range, RegularExpression, StringLength etc.,). ASP.NET MVC uses jquery unobtrusive validation to perform client side validation.

MODEL

using System.ComponentModel.DataAnnotations;
namespace validation.Models
{
public class ValidationModel
{
[Required] [Display(Name = "User name")] [StringLength(20,ErrorMessage = "Name not be exceed 20 char")] public string Name { get; set; } [Required(ErrorMessage = "Email_id must not be empty")] [RegularExpression(".+\@.+\..+", ErrorMessage = "Please Enter valid mail id")] public string Email { get; set; } [Required(ErrorMessage = "Please enter phone number")] [RegularExpression(@"^(?([0-9]{3}))?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$", ErrorMessage = "Enter valid number")] public string Phone { get; set; } [Required(ErrorMessage = "Please enter your address")] public string Address { get; set; } [Range(0,120,ErrorMessage = "Please enter between 0 to 120")] public string Age { get; set; } } }

View

@model validation.Models.ValidationModel
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
@using (Html.BeginForm())
{
@Html.ValidationSummary(false)
<table>
<tr> <td>
Your Name: @Html.TextBoxFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</td> </tr>
<tr> <td>
Your Age: @Html.TextBoxFor(model => model.Age)
@Html.ValidationMessageFor(model => model.Age)
</td></tr>
<tr> <td>
Your Email: @Html.TextBoxFor(model => model.Email)
@Html.ValidationMessageFor(model => model.Email)
</td></tr>
<tr><td>
Your Phone: @Html.TextBoxFor(model => model.Phone)
@Html.ValidationMessageFor(model => model.Phone)
</td></tr>
<tr><td>
Your Address: @Html.TextBoxFor(model => model.Address)
@Html.ValidationMessageFor(model => model.Address)
</td></tr>
<tr><td>
<input type="submit" value="Register Me" />
</td></tr>
</table>
}

OUTPUT:

Validation using Data Annotation Validator 1 - MVC - dotnet-helpers

Below output shows the Email validation and Range validation

NOTE :

Validation for password comparsion

[DataType(DataType.Password)]
[Display(Name = "Confirm new password")]
[Compare("NewPassword", ErrorMessage = "The new password and confirmation password do not match.")]

13 thoughts on “Validation in MVC4 Razor with the Data Annotation Validators in “MODEL””

  1. Pretty excellent post. I just stumbled upon your blog and wanted to say that I have very enjoyed reading your blog posts. Any way I’ll be subscribing for your feed and I hope you write-up again soon.

  2. This site just made my week! I were seeking close to for info on this. I’m glad now that I ran across this webpage. Woohoo!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.