How do you detect Credit card type based on the number?


How do you detect Credit card type based on the number?

How do you detect Credit card type based on the number using JavaScript? Simple solution using only code, no library required.

function getCardType (number) {
  const re = {
    electron: /^(4026|417500|4405|4508|4844|4913|4917)\d+$/,
    maestro: /^(5018|5020|5038|5612|5893|6304|6759|6761|6762|6763|0604|6390)\d+$/,
    dankort: /^(5019)\d+$/,
    interpayment: /^(636)\d+$/,
    unionpay: /^(62|88)\d+$/,
    visa: /^4[0-9]{12}(?:[0-9]{3})?$/,
    mastercard: /^(5[1-5][0-9]{14}|2(22[1-9][0-9]{12}|2[3-9][0-9]{13}|[3-6][0-9]{14}|7[0-1][0-9]{13}|720[0-9]{12}))$/,
    amex: /^3[47][0-9]{13}$/,
    diners: /^3(?:0[0-5]|[68][0-9])[0-9]{11}$/,
    discover: /^6(?:011|5[0-9]{2})[0-9]{12}$/,
    jcb: /^(?:2131|1800|35\d{3})\d{11}$/
  }

  for (var key in re) {
    if (re[key].test(number)) {
      return key
    }
  }
  
  return 'unknown'
}

Test cards

UNIONPAY

8800000000000000

ELECTRON

4026000000000000

4175000000000000

4405000000000000

4508000000000000

4844000000000000

4913000000000000

4917000000000000

DANKORT

5019000000000000

MAESTRO

5018000000000000

5020000000000000

5038000000000000

5612000000000000

5893000000000000

6304000000000000

6759000000000000

6761000000000000

6762000000000000

6763000000000000

0604000000000000

6390000000000000

JCB

3528000000000000

3589000000000000

3529000000000000

INTERPAYMENT

6360000000000000

VISA

4916338506082832

4556015886206505

4539048040151731

4024007198964305

4716175187624512

MASTERCARD

5280934283171080

5456060454627409

5331113404316994

5259474113320034

5442179619690834

DISCOVER

6011894492395579

6011388644154687

6011880085013612

6011652795433988

6011375973328347

AMEX

345936346788903

377669501013152

373083634595479

370710819865268

371095063560404

detect card type by number

Sources:

Newsletter


Related Posts

Get your Jenkins Passwords from secrets

How to get your Jenkins passwords from your secrets credentials. Follow this simple tutoriale and find your password or ssh private key.

How to launch a 6 figures startup with 0$ investment

Are you curious how to launch a startup in the fast and effective way? Here's not the answer, but you can understand how to achieve it

How to cold email

How to cold email - a practical guide about how to use cold email in your activity as a CEO of a software agency or startup owner.

Linux disk space alerts on Discord

Linux disk space alerts. Check your space and be responsible with your project. Take care of your space and don't crack the project.

Git clone private repo in docker

A simple how to guide to use git clone of a private repository inside docker container. With vuejs / nodejs app.

How to detect the browser language in javascript

How to detect the browser language in javascript in just a line of code. The method is working on all major browser

You don't need a macbook pro to be a pro

You don't need a macbook pro to be a pro! You need a laptop and internet connection, and a brain, but you already have it

Free templates for startup

Free HTML templates for startup or a new project. A complete list with free resources to build your next startup's website and gain the traction to the sky.

What I learned to never do in a presentation meeting

What I learned to never do in a presentation meeting. I had a very bad meeting experience with a gateway provider.

Startup graveyard is a series of killed projects that made millions in revenue per year

Startup graveyard is a series of killed projects that made millions in revenue per year. Just watch their graveyard and do it your own. What better validation do you expect.