Bulleting Credit Card Numbers With Regular Expressions

Posted December 6, 2007 at 4:45 PM

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:

 Launch code in new window » Download code as text file »

  • <!---
  • 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:

 Launch code in new window » Download code as text file »

  • <!---
  • 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.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Other Searches  |  Print Page




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 »
7,539 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!


Post Comment  |  Ask Ben

Recent Blog Comments
Mar 18, 2010 at 6:34 PM
Exploring ColdFusion Component Runtime Class Properties And Serialization
@Ben Very useful analyses. Thank you @Elliot Thanks for additional clarification Though, it's quite a shame that getBust() failed...not defined ;) ... read »
Mar 18, 2010 at 5:35 PM
Exploring ColdFusion Component Runtime Class Properties And Serialization
Saving private properties is necessary so that you can "reconstitute" an object on the other side of the wire, or load up a serialized object you saved to disk. If it didn't save the private state o ... read »
Mar 18, 2010 at 4:04 PM
jQuery's Event Triggering, Order Of Default Behavior, And triggerHandler()
Tks! You saved-me today. it can be chained into one statement: $("#x).attr("checked","checked").triggerHandler('click'); ... read »
Mar 18, 2010 at 1:18 PM
Finally Finished Ayn Rand's Atlas Shrugged Audio Book
@joaopft, Not disputing what you say - but... If I understand you correctly, you are saying that Positivism is based on sense experience (what I experience is what is), but Quantum theory states tha ... read »
Mar 18, 2010 at 11:48 AM
Duplicate() Much Faster Than ColdFusion Query-of-Queries
I am working on a massive xml parsing, qofq app to create 2 seperate xml files. I just don't understand the concept/purpose of duplicate function, are you duplicating the data or the row, into a new ... read »
Mar 18, 2010 at 11:22 AM
Exploring ColdFusion Component Runtime Class Properties And Serialization
@Zarko, Ha ha, you know ColdFusion is my first love ;) ... read »
Mar 18, 2010 at 11:15 AM
Exploring ColdFusion Component Runtime Class Properties And Serialization
Hi Ben, nice to have you back! I already gave up on you, thinking you'll write about jQuery and iPhone for the rest our our lives! :) ... read »
Mar 18, 2010 at 10:36 AM
Ask Ben: Javascript Replace And Multiple Lines / Line Breaks
@Ben Nadel, Hey Ben, thanks for you're response. It works!! However.. if you could please kindly look at http://edeals.zzl.org/divchange2.php where I am trying it out you will see that with the " ... read »