DEV Community

Cover image for C# Model Validation Attribute Cheatsheet
Sukhpinder Singh
Sukhpinder Singh

Posted on • Originally published at Medium

C# Model Validation Attribute Cheatsheet

The cheat sheet provides a breakdown of built-in validation attributes in C# with individual code examples for each:

Photo by [Mohammad Rahmani](https://unsplash.com/@afgprogrammer?utm_source=medium&utm_medium=referral) on [Unsplash](https://unsplash.com?utm_source=medium&utm_medium=referral)

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
    }


Enter fullscreen mode Exit fullscreen mode

[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; }
    }


Enter fullscreen mode Exit fullscreen mode

[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; }
    }


Enter fullscreen mode Exit fullscreen mode

[EmailAddress]: Validates email address format.

Example:



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

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


Enter fullscreen mode Exit fullscreen mode

[Phone]: Validates phone number format.

Example:



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

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


Enter fullscreen mode Exit fullscreen mode

[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; }
    }


Enter fullscreen mode Exit fullscreen mode

[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
    }


Enter fullscreen mode Exit fullscreen mode

[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; }
    }


Enter fullscreen mode Exit fullscreen mode

[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; }
    }


Enter fullscreen mode Exit fullscreen mode

[Url]: Validates URL format.

Example:


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

  [Url]
  public string Url { get; set; }
}
Enter fullscreen mode Exit fullscreen mode
Enter fullscreen mode Exit fullscreen mode




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

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

buymeacoffee

Top comments (0)