1 Answers
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,
- Required
Specify a property as required.
1. [Required(ErrorMessage=”CustomerName is mandatory”)]
- RegularExpression
Specifies the regular expression to validate the value of the property.
1. [RegularExpression(“[a-z]”, ErrorMessage = “Invalid character”)]
- Range
Specifies the Range of values between which the property values are checked.
1. [Range(1000,10000,ErrorMessage=”Range should be between 1k & 10k”)]
- 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”)]
- MaxLength
Specifies the Max length for the property value.
1. [MaxLength(10,ErrorMessage=”Customer Code is exceeding”)]
- MinLength
It is used to check for minimum length.
1. [MinLength(5, ErrorMessage = “Customer Code is too small”)]