Saturday, November 8, 2014

Validations in Asp.Net MVC

First we need to add namespace in top of the model class
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema;

Data Annotation validation apply on specific property of model class. here i explain different types of validation attributes example as below.
Required data annotation attribute.



"Required" attribute can have different parameters such as "ErrorMessage" for validation error message statically, "ErrorMessageResourceName" for validation error message key which is came from Global Resource file. You can use either "ErrorMessage" parameter or "ErrorMessageResourceName" and "ErrorMessageResourceType".

In Order to display validation error message in View or Partial View by using "ValidationMessageFor" Html helper.



Lastly, the controller we can check if the model is proper valid or not by using the "ModelState.IsValid" property and accordingly we can redirect to view.



For other validation Attributes I have explain in details as below.

If you want to check string length, you can use "StringLength" attribute
[StringLength(160)]
public string FirstName { get; set; }

If you want to check regular expression, you can use the "RegularExpression" attribute.
[RegularExpression(@"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}")]public string Email { get; set; }

If you want to check whether the numbers are in range, you can use the "Range" attribute.
[Range(10,25)]public int Age { get; set; }

If you want to check Custom Validation for Email whether this email is already registered or not, you can use the "CustomValidation" attribute and for that you can check by action of that controller by Remote attribute.



If you would like to compare the value of one field with another field, we can use the "Compare" attribute.
[Required(ErrorMessageResourceType = typeof(Global), ErrorMessageResourceName = "PasswordRequired")]
[DataType(DataType.Password)]
public string Password { get; set; }
[Required(ErrorMessageResourceType = typeof(Global), ErrorMessageResourceName = "ConfirmPasswordRequired")]
[DataType(DataType.Password)]
[System.ComponentModel.DataAnnotations.Compare("Password", ErrorMessageResourceType = typeof(Global), ErrorMessageResourceName = "PasswordDoNotMatch")]
public string ConfirmPassword { get; set; }


If you would like to check the URL is valid or not, we can use the "Url" attribute.
[Display(Name = "Facebook")]
[Url(ErrorMessageResourceType = typeof(Global), ErrorMessageResourceName = "ValidFaceBookAddress", ErrorMessage = null)]
[DataType(DataType.Url)]
public string Facebook { get; set; }

If you want to check atleast one checkbox checked then you have to use "CheckList" attribute which is creating dynamically
[CheckList(1, false, ErrorMessageResourceType = typeof(Global), ErrorMessageResourceName = "Atleast1PaymetnMode")]
[Display(Name = "Payment Mode")]
public List PaymentMode { get; set; }

I tried my best to explain in details about validtion in MVC still you have any query regarding this question then you can comment or contact us on mehulpatelk152@gmail.com

No comments:

Post a Comment