Skip to content

Instantly share code, notes, and snippets.

@hrahimi270
Forked from mahmoud-eskandari/validateCard.js
Created January 25, 2020 14:01
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hrahimi270/b81c8590bb6043c8d2264acb7853ac46 to your computer and use it in GitHub Desktop.
Save hrahimi270/b81c8590bb6043c8d2264acb7853ac46 to your computer and use it in GitHub Desktop.
اعتبار سنجی کارت عابر بانک ایران در جاوا اسکریپت \ Validation function of Iranian bank cards in Javascript \ تابع تشخیص صحت کارت عابربانک
"use strict";
// By Mahmoud Eskandari @ MIT license
function validateCard(card) {
if (typeof card === 'undefined'
|| card === null
|| card.length !== 16) {
return false;
}
let cardTotal = 0;
for (let i = 0; i < 16; i += 1) {
const c = Number(card[i]);
if (i % 2 === 0) {
cardTotal += ((c * 2 > 9) ? (c * 2) - 9 : (c * 2));
} else {
cardTotal += c;
}
}
return (cardTotal % 10 === 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment