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

Note : Hi guys for more detail related with this article click here.visit new website for future posts http://www.dotnet-helpers.com. Thanks for support

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

ASP.NET MVC uses jquery unobtrusive validation is used  to perform client side validation.

Data Annotation

It common validation attributes  which extend the built-in ASP.NET DataAnnotations (Required, Range, RegularExpression and StringLength).

Namespace does not contain System.Web.DataAnnotations provides a built-in set of validation attributes that you can apply declaratively to any class or property.

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)…………………………………

Note : Hi guys for more detail related with this article click here.visit new website for future posts http://www.dotnet-helpers.com. Thanks for support

 

OUTPUT: ……………………………………

 

 

Advertisements

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

  1. 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!

  2. 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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s