Wingmiester
Wingmiester

Reputation: 15

Regular expression for all bank card numbers

I am using a regular expression to match all UK bank card number formats; I have done research and managed to find/amend a regex that covers the majority of formats. However, I have a bit of an edge case where one is not matching and I do not know why, or how to resolve. This is what I am using:

(\b[4|5|6](\d){3}[\s|-]?((\d){4}[\s|-]?){2}(\d){4}\b)|(\b(\d){4}[\s|-]?(\d){6}[\s|-]?(\d){5}\b)

This is an example card number that does not work: 6759000000005

This is an example card number that does work: 675900000000555

Apologies if this is an easy question, I am fairly new to regular expression syntax. Any help to resolve would be greatly appreciated. Thanks.

Upvotes: 0

Views: 2157

Answers (1)

Tensibai
Tensibai

Reputation: 15784

See here the demo

The regex is (\b[4|5|6]\d{3}[\s-]?(\d{4}[\s-]?){2}\d{1,4}\b)|(\b\d{4}[\s-]?\d{6}[\s-]?\d{5}\b)

I'm not an expert of UK cards, so I can't tell what is the expected format as you did not gave exemples with spaces or hyphens in them... If you can refine the requirements it's handlable.

A more generic card number validation (without separators, so you'll need to strip them before) would be

\d{6}\d{1,12}\d

As per the requirements of the norm (found nothing on the minimum length of the account identifier):

An ISO/IEC 7812 card number is most commonly 16 digits in length,[1] and can be up to 19 digits. The structure is as follows:

a six-digit Issuer Identification Number (IIN) (previously called the "Bank Identification Number" (BIN)) the first digit of which is the Major Industry Identifier (MII), a variable length (up to 12 digits) individual account identifier, a single check digit calculated using the Luhn algorithm.[2]

Upvotes: 1

Related Questions