Skip to content

Instantly share code, notes, and snippets.

public sealed class PropertiesMustMatchAttribute : ValidationAttribute
{
private const string _defaultErrorMessage = "'{0}' and '{1}' do not match.";
private readonly object _typeId = new object();
public PropertiesMustMatchAttribute(string originalProperty, string confirmProperty)
: base(_defaultErrorMessage)
{
OriginalProperty = originalProperty;
ConfirmProperty = confirmProperty;
@LSTANCZYK
LSTANCZYK / NotEqualTo
Created September 25, 2017 01:32 — forked from StevenSwann/NotEqualTo
Data Annotation Custom Validation
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public sealed class NotEqualToAttribute : ValidationAttribute
{
private const string DefaultErrorMessage = "{0} cannot be the same as {1}.";
public string OtherProperty { get; private set; }
public NotEqualToAttribute(string otherProperty)
: base(DefaultErrorMessage)
{
/// <summary>
/// Limit numbers of items in ContentArea
/// Add [MaxItemCount(2)] to a prop-definition limits number of items;
/// [Display(Name = "Articles")]
/// [MaxItemCount(2)]
/// public virtual ContentArea ArticlesContentArea { get; set; }
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public sealed class MaxItemCount : ValidationAttribute
{
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = true)]
public class AtLeastOneTrueAttribute : ValidationAttribute
{
private readonly string _targetProperty;
public AtLeastOneTrueAttribute(string targetProperty)
{
_targetProperty = targetProperty;
}
@LSTANCZYK
LSTANCZYK / UsStateTwoCharacterAbbreviationAttribute.cs
Created September 25, 2017 01:31 — forked from jeremykdev/UsStateTwoCharacterAbbreviationAttribute.cs
Custom data annotation validation attribute to validate that data is a two character U.S. state abbreviation.
using System;
using System.ComponentModel.DataAnnotations;
using System.Linq;
sealed public class UsStateTwoCharacterAbbreviationAttribute : ValidationAttribute
{
private readonly string[] validStateAbbr = new string[] { "AK", "AL", "AR", "AZ", "CA", "CO", "CT", "DC", "DE", "FL", "GA", "HI", "IA", "ID", "IL", "IN", "KS", "KY", "LA", "MA", "MD", "ME", "MI", "MN", "MO", "MS", "MT", "NC", "ND", "NE", "NH", "NJ", "NM", "NV", "NY", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VA", "VT", "WA", "WI", "WV", "WY" };
public sealed class ValidatePasswordLengthAttribute : ValidationAttribute
{
private const string _defaultErrorMessage = "'{0}' must be at least {1} characters long.";
private readonly int _minCharacters = Membership.Provider.MinRequiredPasswordLength;
public ValidatePasswordLengthAttribute()
: base(_defaultErrorMessage)
{
}
@LSTANCZYK
LSTANCZYK / ImageType.cs
Created September 25, 2017 01:31 — forked from andredublin/ImageType.cs
C# class to check image file type
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Text.RegularExpressions;
namespace MvcMovie.Service.AppService
{
public class ImageType : ValidationAttribute
@LSTANCZYK
LSTANCZYK / RequireNonDefaultAttribute.cs
Created September 25, 2017 01:30 — forked from nickalbrecht/CustomValidationAttributeAdapterProvider.cs
Attribute to mark properties backed by non-nullable types (or structs like DateTime & Guid) as requiring a value other than their default value
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)]
public class RequireNonDefaultAttribute : ValidationAttribute
{
public RequireNonDefaultAttribute()
: base("The {0} field requires a non-default value.")
{
}
/// <summary>
/// Override of <see cref="ValidationAttribute.IsValid(object)"/>
@LSTANCZYK
LSTANCZYK / AllowedFileExtentions
Created September 25, 2017 01:30 — forked from StevenSwann/AllowedFileExtentions
Data Annotation Custom Validation Allowed File Extentions
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class AllowedFileExtensionsAttribute : ValidationAttribute
{
private List<string> AllowedExtensions { get; set; }
public AllowedFileExtensionsAttribute(string fileExtensions)
{
AllowedExtensions = fileExtensions.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
}
@LSTANCZYK
LSTANCZYK / FileMaxSizeAttribute.cs
Created September 25, 2017 01:30 — forked from ioab/FileMaxSizeAttribute.cs
File max size validation attribute for ASP.NET MVC
namespace App.Infrastructure
{
public class FileMaxSizeAttribute : ValidationAttribute
{
public FileMaxSizeAttribute(int maxSize = 10 * 1024 * 1024, string errorMessage = "{0} is not a valid file.")
: base(errorMessage)
{
// 10 MB by default //
this._maxSize = maxSize;
}