How Validation works in MVC? What is Data Annotation?

Data validation is a key aspect for developing web application. In Asp.net MVC, we can easily apply validation to web application by using Data Annotation attribute classes to model class. Data Annotation attribute classes are present in System. ComponentModel.

 

Validation can be done in MVC using DATA ANNOTATION Attributes.

[Required]: Specifies that a property is required and cannot be null or empty.

[StringLength]: Specifies the minimum and maximum length of a string property.

[RegularExpression]: Specifies a regular expression pattern that a string property must match.

[EmailAddress]: Specifies that a string property must be a valid email address format.

[Range]: Specifies the minimum and maximum allowed values for a numeric property.

[Compare]: Compares the value of a property with the value of another property in the same model.

[DataType]: Specifies the data type of a property, which can be used for formatting and validation purposes.

[Display]: Provides metadata for displaying the property, such as the name and order of the property in a view.

[Editable]: Specifies whether a property can be edited or displayed in a view.

[ReadOnly]: Specifies that a property can be displayed in a view but cannot be edited.

[HiddenInput]: Specifies that a property should be hidden in a view.

[DisplayName]: Provides a friendly name for a property that can be used in a view.

[DisplayFormat]: Specifies a format string for displaying the value of a property.

[RequiredIf]: Specifies that a property is required if another property in the model has a specific value.

[CreditCard]: Specifies that a string property must be a valid credit card number format.

Post a Comment

Previous Post Next Post