Data Annotation Validator Attributes
DataType
Specify the datatype of a property
Syntax:
[DataType(DataType.Text)]
DisplayName
specifies the display name for a property.
Syntax:
[Display(Name="Student Name")]
3. DisplayFormat
specify the display format for a property like a different format for a Date property.
Syntax:
[DisplayFormat(DataFormatString = "{0:dd.MM.yyyy}")]
Required
Specify a property as required.
Syntax:
[Required(ErrorMessage="Please enter name"),MaxLength(30)]
Regular expression
validates the value of a property by a specified regular expression pattern.
Syntax:
[RegularExpression(@"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$", ErrorMessage = "Email is not valid.")]
Range
validates the value of a property within a specified range of values.
Syntax:
[Range(100,500,ErrorMessage="Please enter correct value")]
StringLength
specifies the min and max length for a string property.
Syntax:
[StringLength(30,ErrorMessage="Do not enter more than 30 characters")]
MaxLength
specifies the max length for a string property.
Syntax:
[MaxLength(3)]
Bind
Specify fields to include or exclude when adding parameter or form values to model properties.
Syntax:
[Bind(Exclude = "StudentID")]
ScaffoldColumn
specifies fields for hiding from editor forms.
How to use Data Annotation Validators
15 Mar 202420 minutes to read
The DataForm component enables users to define the data annotation attributes available from the instance of System.ComponentModel.DataAnnotations.
The DisplayAttribute class is used to specify the display name for a property. The display name is used as the label for the corresponding editor in the DataForm component if label text is not specified for the form item.
The Name property is used to specify the display name for a property. The display name is used as the label for the corresponding editor in the DataForm component if label text is not specified for the form item.
C#
[Display(Name = "First Name")]
public string FirstName { get; set; }
The ShortName property is used to specify the short display name for a property. The short display name is used as the label for the corresponding editor in the DataForm component if label text is not specified for the form item.
C#
[Display(ShortName = "First Name")]
public string FirstName { get; set; }
NOTE
DataForm gives priority to the ShortName property over the Name property of DisplayAttribute and LabelText property of the FormItem.
The Prompt property is used to specify the prompt for a property. The prompt is used as the placeholder for the corresponding editor in the DataForm component.
C#
[Display(Prompt = "Enter your first name")]
public string FirstName { get; set; }
The AutoGenerateField property is used to specify whether the property should be automatically generated as a field in the DataForm component.
C#
[Display(AutoGenerateField = false)]
public string ID { get; set; }
The DataForm component supports the following validation attributes from the System.ComponentModel.DataAnnotations namespace.
The RequiredAttribute class is used to specify that a property is required. The DataForm component displays an error message if the property is empty.
C#
[Required(ErrorMessage = "Name is required")]
public string Name { get; set; }
The RangeAttribute class is used to specify the numeric range constraints for the value of a property. The DataForm component displays an error message if the property value is not within the specified range.
C#
[Range(0, 100, ErrorMessage = "Age must be between 0 and 100")]
public int Age { get; set; }
The RegularExpressionAttribute class is used to specify that a property value must match a specified regular expression. The DataForm component displays an error message if the property value does not match the specified regular expression.
C#
[RegularExpression(@"^[a-zA-Z]+$", ErrorMessage = "Name must contain only alphabets")]
public string Name { get; set; }
The StringLengthAttribute class is used to specify the minimum and maximum length constraints for the value of a property. The DataForm component displays an error message if the property value is not within the specified length constraints.
C#
[StringLength(100, MinimumLength = 3, ErrorMessage = "Name must be between 5 and 10 characters")]
public string Name { get; set; }
The MinLengthAttribute class is used to specify the minimum length constraints for the value of a property. The DataForm component displays an error message if the property value is not within the specified length constraints.
C#
[MinLength(3, ErrorMessage = "Name must be at least 3 characters")]
public string Name { get; set; }
The MaxLengthAttribute class is used to specify the maximum length constraints for the value of a property. The DataForm component displays an error message if the property value is not within the specified length constraints.
C#
[MaxLength(10, ErrorMessage = "Name must be at most 10 characters")]
public string Name { get; set; }
The PhoneAttribute class is used to specify that a property value must match a specified phone number pattern. The DataForm component displays an error message if the property value does not match the specified phone number pattern.
C#
[Phone(ErrorMessage = "Phone number is not valid")]
public string PhoneNumber { get; set; }
The EmailAddressAttribute class is used to specify that a property value must match a specified email address pattern. The DataForm component displays an error message if the property value does not match the specified email address pattern.
C#
[EmailAddress(ErrorMessage = "Email address is not valid")]
public string Email { get; set; }
The UrlAttribute class is used to specify that a property value must match a specified URL pattern. The DataForm component displays an error message if the property value does not match the specified URL pattern.
C#
[Url(ErrorMessage = "URL is not valid")]
public string Url { get; set; }
The EnumDataTypeAttribute class is used to specify that a property value must be a member of the specified enumeration. The DataForm component displays an error message if the property value is not a member of the specified enumeration.
C#
[EnumDataType(typeof(Gender), ErrorMessage = "Please enter a valid gender")]
public string Gender { get; set; }
The CompareAttribute class is used to specify that a property value must match the value of another property in the same class. The DataForm component displays an error message if the property value does not match the value of the other property.
C#
[Compare("Password", ErrorMessage = "Passwords do not match")]
public string ConfirmPassword { get; set; }
The StringLengthAttribute class is used to specify the minimum and maximum length constraints for the value of a property. The DataForm component displays an error message if the property value is not within the specified length constraints.Additionally editor component will not allow to enter more than the specified length.
C#
[StringLength(100, MinimumLength = 3, ErrorMessage = "Name must be between 5 and 10 characters")]
public string Name { get; set; }