Member-only story
C# Model Validation Attribute Cheatsheet
The cheat sheet provides a breakdown of built-in validation attributes in C# with individual code examples for each:
Built-in Attributes:
- [ValidateNever]: Excludes a property or parameter from validation.
- [CreditCard]: Validates credit card format (requires jQuery Validation Additional Methods).
- [Compare(string otherProperty)]: Validates if the property value matches another property value in the model.
- [EmailAddress]: Validates email address format.
- [Phone]: Validates phone number format.
- [Range(double minimum, double maximum)]: Validates if the property value falls within a specified range (inclusive).
- [RegularExpression(string pattern)]: Validates if the property value matches a specified regular expression.
- [Required]: Ensures the property has a value (not null).
- [StringLength(int maximumLength)]: Validates if the string property value doesn’t exceed the specified length.
- [Url]: Validates URL format.
- [Remote(string action, string controller)]: Performs client-side validation by calling a server-side action method.