Advertisement
GeeItSomeLaldy

Credit/Debit card numbers, in an Enum with Regex attributes

Oct 26th, 2016
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.00 KB | None | 0 0
  1. public enum CardType
  2.     {
  3.         Unset= -1,
  4.         Unknown = 0,
  5.         [Description("American Express")]
  6.         [Decoration(@"^3[47][0-9]{13}$")]
  7.         AmexCard = 1,
  8.         [Description("BCGlobal")]
  9.         [Decoration(@"^(6541|6556)[0-9]{12}$")]
  10.         BCGlobal,
  11.         [Description("Carte Blanche")]
  12.         [Decoration(@"^389[0-9]{11}$")]
  13.         CarteBlancheCard,
  14.         [Description("Diners Club")]
  15.         [Decoration(@"^3(?:0[0-5]|[68][0-9])[0-9]{11}$")]
  16.         DinersClub,
  17.         [Description("Discover")]
  18.         [Decoration(@"^65[4-9][0-9]{13}|64[4-9][0-9]{13}|6011[0-9]{12}|(622(?:12[6-9]|1[3-9][0-9]|[2-8][0-9][0-9]|9[01][0-9]|92[0-5])[0-9]{10})$")]
  19.         Discover,
  20.         [Description("Insta Payment")]
  21.         [Decoration(@"^63[7-9][0-9]{13}$")]
  22.         InstaPayment,
  23.         [Description("JCB")]
  24.         [Decoration(@"^(?:2131|1800|35\d{3})\d{11}$")]
  25.         JCB,
  26.         [Description("Korean Local")]
  27.         [Decoration(@"^9[0-9]{15}$")]
  28.         KoreanLocalCard,
  29.         [Description("Laser")]
  30.         [Decoration(@"^(6304|6706|6709|6771)[0-9]{12,15}$")]
  31.         Laser,
  32.         [Description("Maestro")]
  33.         [Decoration(@"^(5018|5020|5038|6304|6759|6761|6763)[0-9]{8,15}$")]
  34.         Maestro,
  35.         [Description("Mastercard")]
  36.         [Decoration(@" ^5[1-5][0-9]{14}$")]
  37.         Mastercard,
  38.         [Description("Mastercard")]
  39.         [Decoration(@"^(?:5[1-5][0-9]\d{1}|222[1-9]|2[3-6][0-9]\d{1}|27[01][0-9]|2‌​720)([\ \-]?)\d{4}\1\d{4}\1\d{4}$")]
  40.         MastercardExtended2016,
  41.         [Description("Solo")]
  42.         [Decoration(@"^(6334|6767)[0-9]{12}|(6334|6767)[0-9]{14}|(6334|6767)[0-9]{15}$")]
  43.         Solo,
  44.         [Description("Switch")]
  45.         [Decoration(@"^(4903|4905|4911|4936|6333|6759)[0-9]{12}|(4903|4905|4911|4936|6333|6759)[0-9]{14}|(4903|4905|4911|4936|6333|6759)[0-9]{15}|564182[0-9]{10}|564182[0-9]{12}|564182[0-9]{13}|633110[0-9]{10}|633110[0-9]{12}|633110[0-9]{13}$")]
  46.         Switch,
  47.         [Description("Union Pay")]
  48.         [Decoration(@"^(62[0-9]{14,17})$")]
  49.         UnionPay,
  50.         [Description("Visa")]
  51.         [Decoration(@"^4[0-9]{12}(?:[0-9]{3})?$")]
  52.         VisaCard,
  53.         [Description("Visa Master Card")]
  54.         [Decoration(@"^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14})$")]
  55.         VisaMasterCard
  56.  
  57.     }
  58.  
  59. public class DecorationAttribute:Attribute
  60.     {
  61.         public object[] names { get; set; }
  62.         public DecorationAttribute(params object[] names)
  63.         {
  64.             this.names = names;
  65.         }
  66.         public static object[] Get<TEnum>(TEnum value)
  67.         {
  68.             FieldInfo fi = value.GetType().GetRuntimeField(value.ToString());
  69.             if (fi != null)
  70.             {
  71.                 DecorationAttribute[] attributes = (DecorationAttribute[])fi.GetCustomAttributes(typeof(DecorationAttribute), false);
  72.                 if ((attributes != null) && (attributes.Length > 0)) return attributes[0].names;
  73.             }
  74.             return new object[] { value.ToString() };
  75.         }
  76.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement