Skip to content

Instantly share code, notes, and snippets.

@atree
Created December 14, 2012 09:15
Show Gist options
  • Save atree/4283939 to your computer and use it in GitHub Desktop.
Save atree/4283939 to your computer and use it in GitHub Desktop.
Quick javascript to validate credit cards (MC, VISA, AMEX, DISCOVER)
cards = ['5555555555554444', '4222222222222', '4111111111111111', '4012888888881881', '378282246310005', '371449635398431', '378734493671000', '6011111111111117']
regex = /^(?:(4[0-9]{12}(?:[0-9]{3})?)|(5[1-5][0-9]{14})|(6(?:011|5[0-9][0-9])[0-9]{12})|(3[47][0-9]{13}))$/
for (c in cards) { console.log(regex.test(cards[c])) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment