Javascript Reference
Categories: tips

javascript Get Credit Card Type By Number (regex validation example)

@May 21, 2009, 5:05 a.m.
document.write('3400 0000 0000 009=' + GetCreditCardTypeByNumber('3400 0000 0000 009'));
document.write('6011 0000 0000 0004=' + GetCreditCardTypeByNumber('6011 0000 0000 0004'));
document.write('5500 0000 0000 0004=' + GetCreditCardTypeByNumber('5500 0000 0000 0004'));
document.write('4111 1111 1111 1111=' + GetCreditCardTypeByNumber('4111 1111 1111 1111'));
 
function GetCreditCardTypeByNumber(ccnumber) {
    var cc = (ccnumber + '').replace(/\s/g, ''); //remove space
 
    if ((/^(34|37)/).test(cc) && cc.length == 15) {
        return 'AMEX'; //AMEX begins with 34 or 37, and length is 15.
    } else if ((/^(51|52|53|54|55)/).test(cc) && cc.length == 16) {
        return 'MasterCard'; //MasterCard beigins with 51-55, and length is 16.
    } else if ((/^(4)/).test(cc) && (cc.length == 13 || cc.length == 16)) {
        return 'Visa'; //VISA begins with 4, and length is 13 or 16.
    } else if ((/^(300|301|302|303|304|305|36|38)/).test(cc) && cc.length == 14) {
        return 'DinersClub'; //Diners Club begins with 300-305 or 36 or 38, and length is 14.
    } else if ((/^(2014|2149)/).test(cc) && cc.length == 15) {
        return 'enRoute'; //enRoute begins with 2014 or 2149, and length is 15.
    } else if ((/^(6011)/).test(cc) && cc.length == 16) {
        return 'Discover'; //Discover begins with 6011, and length is 16.
    } else if ((/^(3)/).test(cc) && cc.length == 16) {
        return 'JCB';  //JCB begins with 3, and length is 16.
    } else if ((/^(2131|1800)/).test(cc) && cc.length == 15) {
        return 'JCB';  //JCB begins with 2131 or 1800, and length is 15.
    }
    return '?'; //unknow type
}
 
function IsValidCC(str) { //A boolean version
    if (GetCreditCardTypeByNumber(str) == '?') return false;
    return true;
}
 

Hello! This is my first visit to your blog! We are a group of volunteers and starting a new project in a community in the same niche. Your blog provided us useful information to work on. You have done a outstanding job!
generic viagra online pharmacy - 3 minutes

Wow, awesome blog layout! How long have you been blogging for? you make blogging look easy. The overall look of your site is wonderful, let alone the content!
canadian pharmaceuticals online - 2 hours
Enjoy Pin-Up Casino Canada! ??
Looking for the best online casino in Canada? Pin-Up Casino Canada is your leading platform for big wins and thrilling games. With hundreds of games, joining Pin-Up Casino Canada -
<a href="https://stanley-cup.ca/" target="_blank">pinup casino online</a> guarantees nonstop excitement!

What Makes Pin-Up Casino Canada Special?

Extensive Game Selection: Choose from exciting table games, designed for every type of player.
Exclusive Bonuses: Claim daily rewards to maximize your chances of winning.
Safe & Secure: Your personal information are protected with trusted technology.
Mobile Compatibility: Play on the go with our responsive platform.
How to Get Started?
Playing at Pin-Up Casino Canada is fast and simple:

Create Your Account: Visit the website Pin-Up Casino app Canada - https://forestlakescountryclub.ca/ and sign up in minutes.
Claim Your Bonus: Boost your bankroll as a new player.
Explore the Games: From live dealers to table games, find your favorites and start playing.
Win Big: Spin the reels or place your bets to enjoy thrilling wins!
? Why Canadian Players Love Pin-Up Casino
Pin-Up Casino Canada is a favorite among players for safe gambling. Whether you prefer poker, roulette, or blackjack, you’ll find unparalleled fun here.

Don’t Wait – Play Today!
Sign up at Pin-Up Casino Canada and win big jackpots! ??
Win big Pin-Up Casino Canada - 10 hours

Powered by Linode.