C# Model Validation Attribute Cheatsheet

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.

Built-in Attributes:

[ValidateNever]: Excludes a property from validation.

Example:

public class ProductModel
{
[Required]
public string Name { get; set; }

[ValidateNever]
public string Description { get; set; } // Description is optional, no validation needed
}

[CreditCard]: Validates credit card format (requires jQuery Validation Additional Methods).

Example:

public class OrderModel
{
[Required]
public string CardholderName { get; set; }

[CreditCard]
public string CardNumber { get; set; }

[Range(1, 12)]
public int ExpiryMonth { get; set; }

[Range(2023, 2030)]
public int ExpiryYear { get; set; }
}

[Compare(string otherProperty)]: Validates if the property value matches another property value.

Example:

public class RegisterModel
{
[Required]
public string Password { get; set; }

[Compare(nameof(Password))]
public string ConfirmPassword { get; set; }
}

[EmailAddress]: Validates email address format.

Example:

public class ContactModel
{
[Required]
public string Name { get; set; }

[EmailAddress]
public string Email { get; set; }
}

[Phone]: Validates phone number format.

Example:

public class CustomerModel
{
[Required]
public string Name { get; set; }

[Phone]
public string PhoneNumber { get; set; }
}

[Range(double minimum, double maximum)]: Validates if the property value falls within a specified range (inclusive).

Example:

public class ProductModel
{
[Required]
public string Name { get; set; }

[Range(10, 100)]
public double Price { get; set; }
}

[RegularExpression(string pattern)]: Validates if the property value matches a specified regular expression.

Example:

public class UserModel
{
[Required]
public string Username { get; set; }

[RegularExpression(@”^[a-zA-Z0-9_]+$”, ErrorMessage = “Username can only contain letters, numbers, and underscores”)]
public string UserName { get; set; } // Typo with different casing, will not match validation
}

[Required]: Ensures the property has a value (not null).

Example:

public class LoginModel
{
[Required]
public string Username { get; set; }

[Required]
public string Password { get; set; }
}

[StringLength(int maximumLength)]: Validates if the string property value doesn’t exceed the specified length.

Example:

public class TweetModel
{
[Required]
public string Text { get; set; }

[StringLength(280, MinimumLength = 1)]
public string Content { get; set; }
}

[Url]: Validates URL format.

Example:

public class WebsiteModel
{
[Required]
public string Name { get; set; }

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

Clap if you believe in unicorns & well-structured paragraphs! 🦄📝

Socials: C# Publication | LinkedIn | Instagram | Twitter | Dev.to

Leave a Reply

Your email address will not be published. Required fields are marked *