Skip to content

Instantly share code, notes, and snippets.

@detroitpro
Created March 26, 2015 16:55
Show Gist options
  • Save detroitpro/be90d0459f98136be39d to your computer and use it in GitHub Desktop.
Save detroitpro/be90d0459f98136be39d to your computer and use it in GitHub Desktop.
cvv
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class CvvAttribute : DataTypeAttribute
{
public CvvAttribute()
: base("cvv")
{
}
public override string FormatErrorMessage(string name)
{
if (ErrorMessage == null && ErrorMessageResourceName == null)
{
ErrorMessage = "CVV is not valid";
}
return base.FormatErrorMessage(name);
}
public override bool IsValid(object value)
{
if (value == null)
{
return true;
}
var regex = new Regex(@"^(?!000)\d{3,4}$", RegexOptions.Compiled | RegexOptions.IgnoreCase);
var asString = value.ToString();
var match = regex.Match(asString).Length > 0;
return match;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment