-
-
Save hrahimi270/b81c8590bb6043c8d2264acb7853ac46 to your computer and use it in GitHub Desktop.
اعتبار سنجی کارت عابر بانک ایران در جاوا اسکریپت \ Validation function of Iranian bank cards in Javascript \ تابع تشخیص صحت کارت عابربانک
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"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