Skip to content

Instantly share code, notes, and snippets.

@jrom
Created May 11, 2012 13:36
Show Gist options
  • Save jrom/2659668 to your computer and use it in GitHub Desktop.
Save jrom/2659668 to your computer and use it in GitHub Desktop.
Credit Card number validation in Coffee Script
# Ported from https://github.com/jzaefferer/jquery-validation/blob/master/jquery.validate.js
creditcard = (value) ->
# accept only spaces, digits and dashes
if /[^0-9 \-]+/.test(value)
return false
nCheck = 0
nDigit = 0
bEven = false
value = value.replace(/\D/g, "")
for n in [(value.length - 1)..0]
cDigit = value.charAt(n)
nDigit = parseInt(cDigit, 10)
if bEven && ((nDigit *= 2) > 9)
nDigit -= 9
nCheck += nDigit
bEven = !bEven
(nCheck % 10) == 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment