Skip to content

Instantly share code, notes, and snippets.

@unixcharles
Forked from masylum/creditcard.coffee
Created May 11, 2012 13:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unixcharles/2659715 to your computer and use it in GitHub Desktop.
Save unixcharles/2659715 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
return unless /[^0-9 \-]+/.test(value)
nCheck = nDigit = 0
bEven = false
checkDigit = (n) ->
cDigit = value.charAt(n)
nDigit = parseInt(cDigit, 10)
if bEven && ((nDigit *= 2) > 9)
nDigit -= 9
nCheck += nDigit
bEven = !bEven
value = value.replace(/\D/g, "")
checkDigit num for num in [0...value.length].reverse
(nCheck % 10) == 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment