Skip to content

Instantly share code, notes, and snippets.

@masylum
Forked from jrom/creditcard.coffee
Created May 11, 2012 13:37
Show Gist options
  • Save masylum/2659685 to your computer and use it in GitHub Desktop.
Save masylum/2659685 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
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 [(value.length - 1)..0])
(nCheck % 10) == 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment