In web applications, domain validation plays a core part. Data entered from the client’s end may not always be correct. Therefore you need to ensure that the data entered by the client is not only validated but is also correct application-logic wise.
This article is about the ASP.NET Model validations. The following three type of validations we can do in ASP.NET MVC web applications:
- HTML validation / JavaScript validation
- ASP.NET MVC Model validation
- Database validation
But the most secure validation is the ASP.NET MVC model validation. In HTML/JavaScript, the validation can break easily, but the model validation can't. In ASP.NET MVC model validations are done using Data Annotation, its inherited System.ComponentModel.DataAnnotations assembly. In ASP.NET MVC 4 provides a different way to build custom validation.
Before you do the ASP.NET MVC model validations you need to add the following reference:
“using System.ComponentModel.DataAnnotations;”
Now you are ready to do the model validations.
In the example, create a new class called “company” under the Model add the preceding references:
Now add the properties. In the following example, I have added various properties that are not directly related to the “company.” 

- [Display(Name = "Company Id")]
- public int CompanyRegisterId { get; set; }

- [Display(Name = "Company Id")]
- [Required (ErrorMessage= "Company Id is Required")]
- public int CompanyRegisterId { get; set; }

In HTML view you do not even need to add a label. Using ASP.NET.MVC Model we can do that easily.
The only thing is, you need to call the relevant attribute in the HTML view using Razor syntax.
The “ComponentModel.DataAnnotations” assembly has many built-in validation attributes, for example:
- Required
- Range,
- RegularExpression ,
- Compare
- StringLength
- Data type
There are many data types the user can select to validate the input. Using this, you can validate for the exact data type as in the following:
- Credit Card number
- Currency
- Custom
- Date
- DateTime
- Duration
- Email Address
- HTML
- Image URL
- Multiline text
- Password
- Phone number
- Postal Code
- Text
- Tine
- Upload
- [Required (ErrorMessage="Company Email address Required")]
- [DataType(DataType.EmailAddress)]
- public string CompanyEmailAddress { get; set; }
- [Required (ErrorMessage="Company Phone number is Required")]
- [DataType(DataType.PhoneNumber)]
- public int CompanyPhoneNumber { get; set; }
- [Range (1,100)]
- [DataType (DataType.Currency)]
- public Decimal MinimumSalaryPerEmp { get; set; }
- [Required]
- [DataType (DataType.MultilineText)]
- public string CompanyDescription { get; set; }
- [Required]
- [DataType (DataType.PostalCode , ErrorMessage = " Please Enter Valid Postal Code")]
- public String PostalCode { get; set; }
- [Required (ErrorMessage="No.of working Hours Required")]
- [Range(2, 10, ErrorMessage = "Please Provide correct range. It should be minimum 2 and not more than 10 ")]
- public int WorkingHours { get; set; }
- [Required]
- [StringLength (10 , MinimumLength =5)]
- [Display(Name = "User name")]
- [RegularExpression (@"(\S\D)+", ErrorMessage =" Space and numbers not allowed")]
- public string UserName { get; set; }
You can compare a user-entered password and re-enter password that are matched using this compare attribute.
- [Required]
- [DataType (DataType.Password)]
- [Display (Name = "Enter Password")]
- public string CurrentPassword { get; set; }
- [Required]
- [Display (Name = "Re-enter Password")]
- [Compare("CurrentPassword" , ErrorMessage = "Please Re-enter Password Again")]
- public string ComparedPassword { get; set; }
- [Display(Name = "Company Founder Name")]
- [DisplayFormat(NullDisplayText = "anonymous")]
- public string CompanyFounder { get; set; }
Now create a controller and a view to apply those validations. In this example I am using a default controller and view. The Index action returns a view that contains a form through that a user can fill in the details of the company and submit it.
Sometimes a user needs to implement validation scenarios that are not provided in the MVC framework.
In these kinds of scenarios you need to implement an “IValidatableObject” interface. Using this, you can conduct an in-depth inspection of the model.
- public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
- {
- if (MinimumSalaryPerEmp < 10 && WorkingHours > 5)
- {
- yield return new ValidationResult("Provide Fair Amount for Workers");
- }
- }
(Important: in the view “ValidationSummary” needs to be true.)
- @Html.ValidationSummary(true))
- [HttpPost]
- public ActionResult Index(Company company)
- {
- if (ModelState.IsValid)
- {
- // TO DO
- return View();
- }
- return View();
- }
- @model SampleModelValidation.Models.Company


Now your web application will be ready to run. Run the website, if incorrect input values are submitted you will then see invalid input error messages.
This validation only applies to the server side. But there is also a need to do validation from the client side as well. When considering client-side validation, reducing the server load will be a great benefit to the user. The ASP.NET MVC Framework will support client-side validations as well.
To do client-side validation, a few JavaScripts can be used. They are:
- jQuery
- jQuery validation library
- jQuery unobtrusive validation library (developed by Microsoft)
When you open a new ASP.NET.MVC project you will get all the JavaScripts in the scripts folder that you want in the client-side validation. First we should set the configuration inside the <appSettings> tag in the web.config file. 
Now we need to add the JavaScript references inside the “BundleConfig.cs” file, creating a new script bundle as shown below.
Then render these scripts in the “_Layout.chtml” file as shown below.
- @Scripts.Render("~/bundles/jqueryval")



Anant DhasPosted Sep 19, 2017, 9:42 PM EDT
Nice One.......!!!
Hamid KhanPosted Sep 18, 2017, 4:23 AM EDT
Nice......................
Gowtham RajamanickamTOP 100Posted Aug 27, 2015, 5:52 AM EDT
good
Debasis SahaTOP 100Posted Aug 24, 2015, 8:31 PM EDT
Nice one..
Shridhar SharmaPosted Aug 24, 2015, 8:42 AM EDT
good one
Sibeesh VenuTOP 100Posted Aug 24, 2015, 5:23 AM EDT
Nice Share :)
Rajeesh MenothPosted Aug 24, 2015, 2:54 AM EDT
Good Buddy..Remove Images and Paste your code here.it help for every one
Ankit BansalPosted Aug 24, 2015, 1:33 AM EDT
nice...
Gowtham KTOP 100Posted Aug 23, 2015, 10:57 PM EDT
Good one
Karthikeyan KPosted Aug 23, 2015, 10:55 PM EDT
Thanks for share sir
Sumit JoshiPosted Aug 23, 2015, 10:39 PM EDT
Thanks for sharing..
Mohammed IbrahimPosted Aug 23, 2015, 10:15 PM EDT
nice
Buddika WiduPosted Aug 23, 2015, 10:12 PM EDT
sure , you can do using custom validation by implementing “IValidatableObject” interface .
Saineshwar BageriPosted Aug 23, 2015, 9:38 PM EDT
sir its good one but you should show with dropdownlist validation and checkbox validation