Skip to content

Instantly share code, notes, and snippets.

@avimar
Forked from etodanik/isracard.js
Last active June 18, 2020 10:04
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 avimar/e7421f96357bf06acc31d952c7baf84e to your computer and use it in GitHub Desktop.
Save avimar/e7421f96357bf06acc31d952c7baf84e to your computer and use it in GitHub Desktop.
Checks the validity of a given Isracard credit card number
/*License: MIT*/
function isracardCheck(num) {//algorithm explanation: https://web.archive.org/web/20140227235803/http://povolotski.me/2013/09/24/isracard-credit-card-number-validation-2/
if(typeof num !== 'number') num=''+num;
if(num.length < 8 || num.length > 9) return false;
var sum=0;
num.split('').forEach(function(val,key){
sum+=parseInt(val,10)*(num.length-key);
})
return sum % 11 == 0;
}
@felixmosh
Copy link

A bit more modern code.

function validateIsracard(value) {
  const num = String(value);
  if (num.length < 8 || num.length > 9) {
    return false;
  }
  const sum = Array.from(num, Number).reduce(
    (sum, val, index) => sum + val * (num.length - index),
    0
  );

  return sum % 11 === 0;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment