Created
March 26, 2015 16:55
-
-
Save detroitpro/be90d0459f98136be39d to your computer and use it in GitHub Desktop.
cvv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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