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  |  Permalink  |  Other Searches  |  Print Page



Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

Dec 6, 2007 at 6:01 PM // reply »
2 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 »
6,516 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
Nov 21, 2009 at 6:47 PM
Hal Helms - Real World Object Oriented Development, Sarasota - Day Five
@charlie griefer, Thank you.. ... read »
Nov 21, 2009 at 5:15 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jose Galdamez, Oh heh yeah I didn't paste the whole code. I should have defined the vars -- my bad. It's fixed thou. Thanks. ... read »
Nov 21, 2009 at 4:49 PM
Styling The ColdFusion 8 WriteToBrowser CFImage Output
Great work yet again Ben! Whilst I didn't use this whole code, I copied some of your regex code for a similar problem with the lack of an alt attribute and unescaped ampersands in CFIMAGE for Railo 3 ... read »
Nov 21, 2009 at 1:13 PM
My First ColdFusion Builder Extension - Encrypting And Decrypting CFM / CFC Files
@Ben, Because I am pedantic, I just want to make sure that everyone knows there is absolutely no encryption going on. There is only encoding and obfuscation. The cfencode tool only obfuscates your C ... read »
Nov 21, 2009 at 12:28 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jody I can't seem to get your code sample to work. If you are still having problems, try this code out and see if it gets you what you wanted. <!--- Comma delimited list with various duplicates ... read »
Nov 21, 2009 at 11:03 AM
Groovy Operator Overloading Does Not Work In The ColdFusion Context
Hi Ben, Thanks for this informative post. Now I am reading ur old posts too ... read »
Nov 21, 2009 at 10:56 AM
HostMySite.com Has The Best ColdFusion Hosting
@Mehul, Yes very nice people, however several downtimes per day which was not acceptable. Hence we had to move out. I am glad you are having good luck with them so far. ... read »