---
title: "Explain Common data annotation functions with examples."  
description: "Explain Common data annotation functions with examples."  
author: "Utpal Vishwas"  
published: 2023-10-20  
updated: 2023-10-22  
canonical: https://www.mindstick.com/forum/160241/explain-common-data-annotation-functions-with-examples  
category: ".net core"  
tags: ["asp.net core", ".net core", "data annotations"]  
reading_time: 3 minutes  

---

# Explain Common data annotation functions with examples.

[Explain](https://www.mindstick.com/forum/157854/what-is-system-debugging-explain-some-system-debugging-tools-used-in-modern-computer-systems) [Common](https://www.mindstick.com/articles/23170/10-most-common-accounting-mistakes-of-small-business) [data annotation](https://www.mindstick.com/articles/12234/validation-using-data-annotation-using-entity-framework) [functions](https://www.mindstick.com/forum/160140/explain-the-role-of-functions-as-a-service-faas-in-serverless-computing) with examples.

## Replies

### Reply by Aryan Kumar

[Data](https://www.mindstick.com/articles/13050/salesforce-aiming-to-dominate-predictive-analytics-with-data-science) annotations in ASP.NET Core are attributes that you can apply to model properties to define validation rules, control the display format, specify data types, and more. They play a crucial role in both validating user input and controlling how data is displayed in your application. Here are some common data annotations along with examples:

## [Required] Data Annotation:

The **[Required]** attribute indicates that a property must have a non-null value.

```plaintext
[Required(ErrorMessage = "Name is required.")]
public string Name { get; set; }
```

## [StringLength] Data Annotation:

The **[StringLength]** attribute specifies the minimum and maximum length of a string property.

```plaintext
[StringLength(50, MinimumLength = 3, ErrorMessage = "Name must be between 3 and 50 characters.")]
public string Name { get; set; }
```

## [Range] Data Annotation:

The **[Range]** attribute is used to validate that a numeric property falls within a specified range.

```plaintext
[Range(1, 100, ErrorMessage = "Price must be between 1 and 100.")]
public decimal Price { get; set; }
```

## [RegularExpression] Data Annotation:

The **[RegularExpression]** attribute enforces that a string property matches a specific regular expression pattern.

```plaintext
[RegularExpression(@"^[A-Z]{3}\d{3}$", ErrorMessage = "Must be in the format ABC123.")]
public string Code { get; set; }
```

## [EmailAddress] Data Annotation:

The **[EmailAddress]** attribute validates that a string property contains a valid email address.

```plaintext
[EmailAddress(ErrorMessage = "Invalid email address.")]
public string Email { get; set; }
```

## [Url] Data Annotation:

The **[Url]** attribute ensures that a string property contains a valid URL.

```plaintext
[Url(ErrorMessage = "Invalid URL.")]
public string Website { get; set; }
```

## [Compare] Data Annotation:

The **[Compare]** attribute checks if two properties have the same value. It's commonly used for confirming passwords.

```plaintext
[Compare("Password", ErrorMessage = "Passwords do not match.")]
public string ConfirmPassword { get; set; }
```

## [Display] Data Annotation:

The **[Display]** attribute specifies the display name for a property and can be used to customize how the property's name appears in views.

```plaintext
[Display(Name = "Full Name")]
public string Name { get; set; }
```

## [DataType] Data Annotation:

The **[DataType]** attribute defines the data type of a property, which affects how it's displayed in views.

```plaintext
[DataType(DataType.Date)]
public DateTime BirthDate { get; set; }
```

## [Key] Data Annotation:

The **[Key]** attribute indicates that a property is the primary key in the database.

```plaintext
[Key]
public int ProductId { get; set; }
```

These are just a few examples of common data annotations in ASP.NET Core. They help ensure data integrity, validate user input, and control how data is presented in your application, ultimately improving the user experience and data quality.


---

Original Source: https://www.mindstick.com/forum/160241/explain-common-data-annotation-functions-with-examples

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
