Validation attributes can be used to easily configure model validation.
public class MyModel
{
public int id { get; set; }
//sets the FirstName to be required, and no longer than 100 characters
[Required]
[StringLength(100)]
public string FirstName { get; set; }
}
The built in attributes are:
[CreditCard]
: Validates the property has a credit card format.[Compare]
: Validates two properties in a model match.[EmailAddress]
: Validates the property has an email format.[Phone]
: Validates the property has a telephone format.[Range]
: Validates the property value falls within the given range.[RegularExpression]
: Validates that the data matches the specified regular expression.[Required]
: Makes a property required.[StringLength]
: Validates that a string property has at most the given maximum length.[Url]
: Validates the property has a URL format.