What is Data Annotation Validator Attributes in MVC?

Category: C SharpWhat is Data Annotation Validator Attributes in MVC?
Editor">Editor Staff asked 2 years ago

What is Data Annotation Validator Attributes in MVC?

1 Answers
Editor">Editor Staff answered 2 years ago

Using the Data Annotation Validator Attributes
 
DataAnnotation plays a vital role in added validation to properties while designing the model itself. This validation can be added for both the client side and the server side.
 
You understand that decorating the properties in a model with an Attribute can make that property eligible for Validation.
 
Some of the DataAnnotation used for validation are given below,
 
 

 
Most Asked ASP.NET MVC Interview Questions and Answers

  1. Required
     
    Specify a property as required.
     
    1. [Required(ErrorMessage=”CustomerName is mandatory”)]
     
  2. RegularExpression
     
    Specifies the regular expression to validate the value of the property.
     
    1. [RegularExpression(“[a-z]”, ErrorMessage = “Invalid character”)]
     
  3. Range
     
    Specifies the Range of values between which the property values are checked.
     
    1. [Range(1000,10000,ErrorMessage=”Range should be between 1k & 10k”)]
     
  4. StringLength
     
    Specifies the Min & Max length for a string property.
     
    1. [StringLength(50, MinimumLength = 5, ErrorMessage = “Minimum char is 5 and maximum char is 10”)]
     
  5. MaxLength
     
    Specifies the Max length for the property value.
     
    1. [MaxLength(10,ErrorMessage=”Customer Code is exceeding”)]
     
  6. MinLength
     
    It is used to check for minimum length.
     
    1. [MinLength(5, ErrorMessage = “Customer Code is too small”)]