Bulleting Credit Card Numbers With Regular Expressions

Posted December 6, 2007 at 4:45 PM by Ben Nadel

Tags: ColdFusion

Yesterday, I was working on a little eCommerce site that output the credit card number as part of the user's invoice. Naturally, for security reasons, we are not outputting the entire credit card number in the invoice, but rather a number of bullets (asterisk) and then the last 5 digits. So, for example, the credit card number:

0123456789123456

... would be output as such in the invoice sent to the user:

***********23456

Normally, I would perform this operation with the use of ColdFusion's RepeatString() function married with a little Right() action:

  • <!---
  • Set up the credit card number. This number might
  • be 14 numbers, it might be 16 numbers, I don't know
  • actually know what the difference is among the
  • different card types.
  • --->
  • <cfset strNumber = "0123456789123456" />
  •  
  • <!---
  • Using traditional approach to block out credit card
  • numbers using RepeatString and Right().
  • --->
  • <cfset WriteOutput(
  • RepeatString( "*", (Len( strNumber ) - 5) ) &
  • Right( strNumber, 5 )
  • ) />

This repeats the asterisk character N-5 times and then appends the last 5 digits of the number. This works, but it just always felt a bit ganky to me. I always felt like there should be a regular expression way to do this. Then, yesterday, out of nowhere, it dawned on me: the positive look-ahead.

In regular expressions, a positive look-ahead defines a pattern as matching if and only if the pattern is also followed by the given look-ahead pattern. The look-ahead pattern is, itself, not captured in the matched pattern. Using this, we can replace every character that also has 5 characters ahead of it:

  • <!---
  • Set up the credit card number. This number might
  • be 14 numbers, it might be 16 numbers, I don't know
  • actually know what the difference is among the
  • different card types.
  • --->
  • <cfset strNumber = "0123456789123456" />
  •  
  • <!---
  • Use a single expression to block out credit card
  • numbers us regular expressions.
  • --->
  • #REReplace(
  • strNumber,
  • ".(?=.{5})",
  • "*",
  • "all"
  • )#

This outputs the bulleted credit card number properly.

As far as speed goes, I don't think it really matters on such a small task (and I don't have time to run tests); I find the regular expression to be a bit more elegant if for no other reason, the number of digits to leave at the tail end is defined in only one place as opposed to two places in the old school method. Plus, the code just looks much more simple.



Reader Comments

Dec 6, 2007 at 6:01 PM // reply »
3 Comments

This is great, I added something to just in case the number has the dashes are in the string
<cfset strNumber = "0123-4567-8912-3456" />
<cfset strCC = REReplace(strNumber,"[0-9](?=.*.{4})", "*", "all")>
<cfoutput>
#strCC#
</cfoutput>

Now what I would like to do is if the dashes are not in the string add them which I could do by counting characters but I would like to figure out a way to add the dashes using regEx.


Dec 6, 2007 at 6:26 PM // reply »
10 Comments

Ben,
This is a pretty cool technique, but I still prefer your original code. Why? Because I feel that code ought to mirror the business logic that it's representing. While the regular expression is possibly faster and more elegant, it doesn't communicate the business logic as well as your first example. And, it's harder to understand for someone who doesn't understand RE that well (such as yours truly).

The only change I'd make is adding a static variable to store the number of digits to show. That way if you decide to change it to 4 later on, you don't need to change every line.


Dec 7, 2007 at 1:21 AM // reply »
4 Comments

Regex is great because it's so powerful and yet so succinct, and RepeatString is a cool function.

However, I'm not sure this problem requires even regex:

<cfset strNumber = "0123456789123456" />
<cfset strCC = "***********#Right(strNumber,5)#">

(Does it really matter if the asterisks match the exact number of credit card digits? No. =)

You could use regex if you needed to get rid of dashes..

<cfset strNumber = "0123-4567-8912-3456" />
<cfset strCC = "***********#Right(REReplace(strNumber,"\D","","all"),5)#">

But if you're getting the credit card number from the database, you probably would have already stripped the dashes before you stored it in the database.

Btw, you usually wouldn't save the full credit card number to the database anyway (do you want that liability?), only the last 4-5 digits (which makes the function moot =).

- If you did need to save the full credit card number to the database, you would encrypt it, so the function above would need a decrypt element to it, as well.

But like I say, you would rarely need to store the full CC number in the database, even if you're doing recurring billing, because Gateways often allow you to debit the credit card without the full credit card number if you've already given them the full number previously.


Dec 7, 2007 at 7:13 AM // reply »
11,238 Comments

@Toby,

I agree with you there. The REReplace() is not exactly "clear" code. I was happy because I always *thought* there was a regular expression way to do this, but could never figure it out. It was more a mental breakthrough than a better approach.

@Adam,

Good point on both whether or not the *'s need to line up with the number of digits AND with the fact that we should only be storing the last few numbers anyway. Most excellent feedback.


Nov 13, 2009 at 10:38 AM // reply »
1 Comments

Süper Güet!


Mar 3, 2011 at 7:58 AM // reply »
1 Comments

This is GREAT, information on credit cards is often ignored but it is extremely needed such as http://tips-money-managememt.blogspot.com/2011/01/credit-cards.html


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 17, 2013 at 7:42 PM
HashKeyCopier - An AngularJS Utility Class For Merging Cached And Live Data
Ben - thanks so much for posting these Angular articles and findings, they've been a huge help towards learning one of the more 'complex' JavaScript frameworks out there (IMO). I have been using Angu ... read »
May 16, 2013 at 5:01 PM
UPDATE: Parsing CSV Data Files In ColdFusion With csvToArray()
Your code was the closest thing I've found to obtaining some direction for converting ISO fields to values that CF can translate properly. Thank you for posting! ... read »
May 15, 2013 at 10:37 PM
Very Simple Pusher And ColdFusion Powered Chat
hi id making plz easy ... read »
May 15, 2013 at 6:07 PM
Making SOAP Web Service Requests With ColdFusion And CFHTTP
Ben, you once again saved my bacon at work. Thank you, thank you, thank you! ... read »
May 15, 2013 at 4:15 PM
What If All User Interface (UI) Data Came In Reports?
@Josh, Thanks! @Ben, I definitely recommend the David West book "Object Thinking" I've been quoting from. It goes deeply into the philosophy and history of OO programming. His breadth ... read »
May 15, 2013 at 11:36 AM
Ask Ben: Print Part Of A Web Page With jQuery
I found this helpfull when you need to keep (refresh) the original parent page after closing the iframe child print dialog (Hoping you're not using a form at this time so it won't submit again): On ... read »
May 14, 2013 at 7:13 PM
What If All User Interface (UI) Data Came In Reports?
@Jonah, If there's any books you'd recommend on the subject of domain modelling, I'd love to hear it. I just downloaded the free PDF of "Domain Driven Design Quickly". Figured I'd give it ... read »
May 14, 2013 at 6:57 PM
The UX Of Prototyping: Low-Fidelity Is The New High-Fidelity
@Phillip, I'm not sure I follow what you mean? Are you saying that you looked at the list of widgets provided by the jQuery UI and let that be your style guide? ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools