Data Annotations In ASP.NET MVC

Data Annotations are nothing but certain validations that we put in our models to validate the input from the user. ASP.NET MVC provides a unique feature in which we can validate the models using the Data Annotation attribute. Import the following namespace to use data annotations in the application.

System.ComponentModel.DataAnnotations

DATA ANNOTATION ATTRIBUTES

Here, is a list of some important Data Annotation Attributes.1. RequiredSpecifies that Input field cannot be empty.
Example:

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

2. DisplayNameSpecifies the Display Name for a Property.
Example:

[DisplayName("Enter Your Name: ")]
public string Name { get; set; }

3. StringLengthSpecifies minimum and maximum length for a property.
Example:

[StringLength(50, MinimumLength = 3)]
public string Name { get; set; }

4. RangeSpecifies a range of numeric value.
Example:

[Range(1,120, ErrorMessage ="Age must be between 1-120 in years.")]
public int Age { get; set; }

5. BindInclude or Exclude value when adding form values to model properties.
Example:

[Bind(Exclude = "Id")]

6. ScaffoldColumnSpecifies a field for hiding from editor forms.
Example:

[ScaffoldColumn(false)]
public int Id { get; set; }

7. DisplayFormatSpecifies a display format for a property like Date Format, Currency format etc.
Example:

[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy hh:mm:ss tt}")]
public System.DateTime? HireDate { get; set; }

8. ReadOnlyIt set a property to read-only.
Example:

[ReadOnly(true)]
public string Name { get; private set; }

9. MaxLengthSpecifies max length of string.
Example:

[MaxLength(50)]
public string Name { get; set; }

10. CreditCardspecifies that a data field value is credit card number.
Example:

[DataType(DataType.CreditCard)]
public string Name { get; set; }

11. Comparecompare with other input fields.
Example:

[System.ComponentModel.DataAnnotations.Compare("Email", ErrorMessage = "Email Not Matched")]
public string ConfirmEmail { get; set; }

12. EmailAddressspecifies that an input field value is well-formed Email Address using Regular Expression.
Example:

[Required(ErrorMessage = "Email ID is Required")][DataType(DataType.EmailAddress)][MaxLength(50)][RegularExpression(@ "[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}", ErrorMessage = "Incorrect Email Format")]
public string Email { get; set;}

13. Phonespecifies that an input field value is well-formed phone number using Regular Expression.
Example:

[DataType(DataType.PhoneNumber)]
[RegularExpression(@"^\(?([0-9]{2})[-. ]?([0-9]{4})[-. ]?([0-9]{3})[-. ]?([0-9]{3})$", ErrorMessage = "Not a valid Phone number")]
public string Name { get; set; }

Output format:91-1234-567-89014. Urlspecifies URL validation.
Example:

[Url]
[Required]
public string URL { get; set; }

15. RegularExpressionspecifies that input field is matched with desired Regular Expression.
Example:

[RegularExpression(@"[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}", ErrorMessage = "Incorrect Email Format")]
public string Email { get; set; }

16. DataTypeprovides a list of data type that is associated with input field and parameter.
Example:

[DataType(DataType.EmailAddress)]
public string Email { get; set; }

17. HiddenInput specifies a hidden input field.
Example:

[System.Web.Mvc.HiddenInput(DisplayValue = false)]
public string Name { get; set; }
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using System.Web.Mvc;
 
namespace Validation.Models
{
    [Bind(Exclude = "Id")]   
    public class StudentDetail
    {
        public int Id { get; set; }
 
        [Required(ErrorMessage = "Name is Required")]
        [StringLength(50,MinimumLength =3)]
        public string Name { get; set; }
 
        [Required(ErrorMessage ="Email ID is Required")]
        [DataType(DataType.EmailAddress)]
        [MaxLength(50)]
        [RegularExpression(@"[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}", ErrorMessage = "Incorrect Email Format")]
        public string Email { get; set; }
                
        [Required(ErrorMessage ="Confirm Email is Required")]
        [DataType(DataType.EmailAddress)]
        [System.ComponentModel.DataAnnotations.Compare("Email", ErrorMessage = "Email Not Matched")]
        public string ConfirmEmail { get; set; }
 
        [Required(ErrorMessage ="Age is Required")]
        [Range(1,120, ErrorMessage ="Age must be between 1-120 in years.")]        
        public int Age { get; set; }
    }
}
@Html.TextBoxFor(m => m.Name)
@Html.ValidationMessageFor(m => m.Name)
0 0 vote
Article Rating

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x