Skip to content

Instantly share code, notes, and snippets.

@mildfuzz
Created October 11, 2012 09:14
Show Gist options
  • Select an option

  • Save mildfuzz/3871188 to your computer and use it in GitHub Desktop.

Select an option

Save mildfuzz/3871188 to your computer and use it in GitHub Desktop.
isCreditCard.js
String.prototype.isCreditCard = function()
{
//Credit - https://sites.google.com/site/abapexamples/javascript/luhn-validation
var luhnArr = [[0,2,4,6,8,1,3,5,7,9],[0,1,2,3,4,5,6,7,8,9]], sum = 0;
this.replace(/\D+/g,"").replace(/[\d]/g, function(c, p, o){
sum += luhnArr[ (o.length-p)&1 ][ parseInt(c,10) ];
});
return (sum%10 === 0) && (sum > 0);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment