4

I'm having no end of trouble coming up with an appropriate regex or set of regex's.

What I want to do is detect:

  • Detect contineous run of digits of length 13 through 19
  • Detect contineous run of digits interspersed with whitespace of length 13 through 19
  • Detect contineous run of digits interspersed with dashes of length 13 through 19

The basic business requirement is to warn a user that they may have entered a credit card number in a text field and they ought not to do that (though only a warning, not a hard error). The text field could span multiple lines, could be up to 8k long, a CC # could be embedded anywhere (unlikely to split across multiple lines), could be more than 1 CC# (though detecting the prescence of at least 1 is all I need. I don't need the actual value). Don't need to validate check digit.

The length check can be done external... ie I'm happy to loop through a set of matches, drop any whitespace/dashes and then do a length comparison.

But... JavaScript regex is defeating my every attempt (just not in the right "head space") so I thought I'd ask here :)

Thanks!

user53794
  • 3,800
  • 2
  • 30
  • 31
  • 1
    This seems to be a really odd requirement. So, if I understand correctly, you have a web page with form fields on it. None of those fields are credit card fields, but for some reason you want to make sure that a credit card number isn't entered into any of them... I'm guessing this is just in case the user got confused by your UI and thought they needed to put in some type of payment information? – NotMe Jul 23 '09 at 23:18
  • @chirs lively: Just about right :). It's more of a workflow scenario and someone may decided to add a note that includes the customer's cc# (don't get me wrong it sounds nutty to me too... but what are ya gonna do?). So... popup a warning reminding folks "please don't do that" doesn't seem like a completely unreasonable need. – user53794 Jul 23 '09 at 23:42

5 Answers5

2

Here are all the rules required for credit card validation. You should be able to easily do this in Javascript.

Community
  • 1
  • 1
cletus
  • 616,129
  • 168
  • 910
  • 942
1

Seems like a fairly simple regex. I've made your requirements a little more stringent - as it was, you'd match lists of dates, such as "1999-04-02 2009-12-09 2003-11-21". I've assumed the sections will be in three to six groups and will themselves be groups of three to eight numbers/dashes/whitespace. You can tune these numbers fairly easily.

/[0-9]{13,19}|([0-9- ]{3,8}){3,6}/

I'm not sure this is what you want, but I figured it was worth a go. If this isn't what you're looking for, perhaps you could show us some regexes that come close or give an impression of what you want?

Samir Talwar
  • 14,220
  • 3
  • 41
  • 65
1

You want to regex 8k of text with Javascript? Don't waste your time (or the users poor CPU) doing this with Javascript. You're gonna need server-side validation anyways, so just leave it to that.

Josh Stodola
  • 81,538
  • 47
  • 180
  • 227
  • 8K is the upper limit, and very unlikely to be reached. I would not think a regex against 8K would bring a PC to it's knees for any amount of time. Are you certain of this? – user53794 Jul 24 '09 at 15:28
  • The bottom line is that server-side validation is required. You are gaining *very* little by subjecting the client to such operations. – Josh Stodola Jul 24 '09 at 15:41
1

Here is a great script for many credit card validation steps.

http://javascript.internet.com/forms/val-credit-card.html

Chris Harris
  • 4,705
  • 3
  • 24
  • 22
0
/(?:\d[ -]?){12,18}\d/

Checks for 13-19 digits, each of which digit (except the last) may have a space or dash after it. This is a very liberal regex, though, and you may want to narrow it down to known actual credit card formats.

Nick Lewis
  • 4,150
  • 1
  • 21
  • 22