Is Valid Credit Card Type - CSharp System

CSharp examples for System:String Match

Description

Is Valid Credit Card Type

Demo Code


using System.Collections;
using System.Security.Cryptography;
using System.Xml;
using System.Globalization;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
using System.Web;
using System.Text;
using System.Collections.Generic;
using System;/*from  w ww  . ja  v  a2  s .  c o m*/

public class Main{
        public static bool IsValidCreditCardType(string cardNumber)
        {
            // AMEX -- 34 or 37 -- 15 length
            if ((Regex.IsMatch(cardNumber, "^(34|37)")) && ((_CreditCardTypes & CreditCardType.Amex) != 0))
                return (15 == cardNumber.Length);

                // MasterCard -- 51 through 55 -- 16 length
            else if ((Regex.IsMatch(cardNumber, "^(51|52|53|54|55)")) && ((_CreditCardTypes & CreditCardType.MasterCard) != 0))
                return (16 == cardNumber.Length);

                // VISA -- 4 -- 13 and 16 length
            else if ((Regex.IsMatch(cardNumber, "^(4)")) && ((_CreditCardTypes & CreditCardType.VISA) != 0))
                return (13 == cardNumber.Length || 16 == cardNumber.Length);

                // Diners Club -- 300-305, 36 or 38 -- 14 length
            else if ((Regex.IsMatch(cardNumber, "^(300|301|302|303|304|305|36|38)")) && ((_CreditCardTypes & CreditCardType.DinersClub) != 0))
                return (14 == cardNumber.Length);

                // enRoute -- 2014,2149 -- 15 length
            else if ((Regex.IsMatch(cardNumber, "^(2014|2149)")) && ((_CreditCardTypes & CreditCardType.DinersClub) != 0))
                return (15 == cardNumber.Length);

                // Discover -- 6011 -- 16 length
            else if ((Regex.IsMatch(cardNumber, "^(6011)")) && ((_CreditCardTypes & CreditCardType.Discover) != 0))
                return (16 == cardNumber.Length);

                // JCB -- 3 -- 16 length
            else if ((Regex.IsMatch(cardNumber, "^(3)")) && ((_CreditCardTypes & CreditCardType.JCB) != 0))
                return (16 == cardNumber.Length);

                // JCB -- 2131, 1800 -- 15 length
            else if ((Regex.IsMatch(cardNumber, "^(2131|1800)")) && ((_CreditCardTypes & CreditCardType.JCB) != 0))
                return (15 == cardNumber.Length);
            else
            {
                // Card type wasn't recognised, provided Unknown is in the CreditCardTypes property, then
                // return true, otherwise return false.
                return ((_CreditCardTypes & CreditCardType.Unknown) != 0);
            }
        }
}

Related Tutorials