Regex Credit Card Validation quick test

Started by spl, May 01, 2025, 11:28:53 AM

Previous topic - Next topic

spl

Attached is best guess for several standard Credit Cards with regex patterns suggested on multiple sites. Appreciate any additions or generated errors:
;Winbatch 2025A - Regex Credit Card Validation
;Stan Littlefield, 5/1/2025
;For common cards, additional patterns may be added for other card types
;========================================================================
IntControl(73,1,0,0,0)
gosub udfs
ObjectClrOption( 'useany', 'System')
opts = ObjectClrType('System.Text.RegularExpressions.RegexOptions',1)
;for testing select from below
;card = '5555555555554444'
;card = '4012888888881881' 
;card = '371449635398431'
card = '38520000023237'
;card = '6011000990139424'
;card = '3566002020360505'
;card = '979884432'
text = "This service will authenticate this card: ":card
Message("Text to Search",text)
results = ""
retval = @false
pattern = '\b(?:5[1-5][0-9]{2}|222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}$'
if ! retval Then regex(card,'MasterCard')
pattern = '\b4[0-9]{12}(?:[0-9]{3})?$'
if ! retval Then regex(card,'Visa')
pattern = '\b3[47][0-9]{13}$'
if ! retval Then regex(card,'American Express')
pattern = '\b3(?:0[0-5]|[68][0-9])[0-9]{11}$'
if ! retval Then regex(card,'Diners Club')
pattern = '\b6(?:011|5[0-9]{2})[0-9]{12}$'
if ! retval Then regex(card,'Discover')
pattern = '\b(?:2131|1800|35\d{3})\d{11}$'
if ! retval Then regex(card,'JCB')
if ! retval Then results = "No Match for Card"
Message(card,results)
Exit
;========================================================================

:WBERRORHANDLER
geterror()
Terminate(@TRUE,"Error Encountered",errmsg)

;========================================================================
:udfs
#DefineSubRoutine geterror()
   wberroradditionalinfo = wberrorarray[6]
   lasterr = wberrorarray[0]
   handlerline = wberrorarray[1]
   textstring = wberrorarray[5]
   linenumber = wberrorarray[8]
   errmsg = "Error: ":lasterr:@LF:textstring:@LF:"Line (":linenumber:")":@LF:wberroradditionalinfo
   Return(errmsg)
#EndSubRoutine

#DefineSubRoutine regex(card,holder)
   oReg = ObjectClrNew('System.Text.RegularExpressions.Regex')
   oReg.CacheSize = ObjectType("ui2",30)
   matches = oReg.Matches(text,pattern,opts) 
   if matches.Count>0
      foreach match in matches
         results := "Card is valid ":holder:@LF 
      Next
      retval = @true
   endif
   oReg=0
   Return(results)
#EndSubRoutine

Return
;========================================================================
Stan - formerly stanl [ex-Pundit]

td

You can also use the xVerifyCCard function provided by the WILx extender.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

spl

Quote from: td on May 01, 2025, 05:38:00 PMYou can also use the xVerifyCCard function provided by the WILx extender.

From the WB Help:  It doesn't verify that the number is real, working credit card number. It only verifies that the number could be a valid credit card number.

but to be fair, RegEx is not 100% accurate, can be tricky and not really a working number. Both Visa and Mastercard have changed their formats, i.e. number ranges for certain sections of the card in recent years. Additionally, the script probably should include stripping of non-numerics in the input and checks for expiration date/3-digit verification.
Stan - formerly stanl [ex-Pundit]

td

It is a simple function, but it has worked amazingly well over the years.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

spl

Quote from: td on May 02, 2025, 08:23:29 AMIt is a simple function, but it has worked amazingly well over the years.

Whoopee! I was just trying to push the obvious a little further, maybe get some more users interested in CLR or Regex.
Stan - formerly stanl [ex-Pundit]

td

Quote from: spl on May 02, 2025, 09:24:14 AMWhoopee! I was just trying to push the obvious a little further, maybe get some more users interested in CLR or Regex.

Whoopee!? I am sure users will appreciate your insights.

We use a service that checks card numbers with no cost to us unless we submit a charge. The xVerifyCCard simply catches some obvious problems before the number is submitted to the service.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

spl

Quote from: td on May 02, 2025, 12:47:45 PMWe use a service that checks card numbers with no cost to us unless we submit a charge.

I would hope so. A simple regex script or WB function could not. Obviously not the point of my script.
Stan - formerly stanl [ex-Pundit]

td

Just trying to add context and expand the topic a bit. Not a topic that appears on this forum, but we do have users interested in credit card verification.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

spl

Quote from: td on May 02, 2025, 01:56:24 PMJust trying to add context and expand the topic a bit. Not a topic that appears on this forum, but we do have users interested in credit card verification.

Understood. And it is useful to distinguish between validation and verification. I pasted together the regex for individual card holders although there are several generic regex patterns to cover the majority. Was thinking about situations where only certain cards are acceptable. Then, there is PayPal.
Stan - formerly stanl [ex-Pundit]

SMF spam blocked by CleanTalk