Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi i want code in regex or vb.net that match credit card with NUMBER+EXPIRE+CVV in one regex line what I done

\d{4}-?\d{4}-?\d{4}-?\d{4}

credit card number MATCH
(0[1-9]|1[0-2])\/?([0-9]{4}|[0-9]{2})

EXPIRE MATCH
[0-9]{3}$

CVV MATCH
I need credit card match only if match like this

(!chk) beginning word (n) credit card number (M) month (y) year (X) cvv

!chk nnnnnnnnnnnnnnn mmyy xxx
or

!chk nnnnnnnnnnnnnnn mm|yy xxx
or

!chk nnnnnnnnnnnnnnn mm/yy xxx
or

!chk nnnnnnnnnnnnnnn mm\yy xxx
or

!chk nnnnnnnnnnnnnnn mmyyyy xxx
regex

What I have tried:

\d{4}-?\d{4}-?\d{4}-?\d{4}

     credit card number MATCH
(0[1-9]|1[0-2])\/?([0-9]{4}|[0-9]{2})

     EXPIRE MATCH
[0-9]{3}$
Posted
Updated 25-Sep-19 11:06am

Using RegEx for Credit Cards is at best rudimentary; you are better off validating what you are actually working with

Credit Card numbers themselves are generally at least 16 digits long; the primary exception being American Express which uses 15 digits.

CVV numbers are 3 digits long for most; and again, American Express is the outlier with 4 digits.

Dates are 2x2 digits.

Now for validating....
Identify the card being presented-
It it a card that your business will accept?
Is it the correct length?
Does it pass the LUHN algorithm check.

CVV only needs to match the correct length; which depends on identifying the card type

Expiration date needs to be a valid month and date, AND it must be in the future
 
Share this answer
 
Credit card is simple 12 16 digits. <- sorry, not 12 but 16
MM is something like (0[1-9]|1[012]) - i.e 01-09 or 10-12
YY is 2 digits, but you may need to limit their range.
CVV is 3 digits
 
Share this answer
 
v2
Comments
Member 14603662 25-Sep-19 10:56am    
so how can be merge regex in one line
Richard MacCutchan 25-Sep-19 11:45am    
"Put them together and what have you got?"

See RegExr: Learn, Build, & Test RegEx[^] where you can test it.
Quote:
i want code in regex or vb.net that match credit card with NUMBER+EXPIRE+CVV in one regex line

What about putting the 3 parts together with a space in between?
\d{4}-?\d{4}-?\d{4}-?\d{4}\s(0[1-9]|1[0-2])\/?([0-9]{4}|[0-9]{2})\s[0-9]{3}$


Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
Online regex tester and debugger: PHP, PCRE, Python, Golang and JavaScript[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx: Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
This site also show the Regex in a nice graph but can't test what match the RegEx: Regexper[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900