Last active
June 22, 2024 19:05
-
-
Save mahmoud-eskandari/1041cc435f5837b540e69f5312165eb1 to your computer and use it in GitHub Desktop.
گولنگ و پی اچ پی اعتبار سنجی کارت عابر بانک ایران در جاوا اسکریپت , Iranian bank cards validator function, Javascript Golang php ,تابع تشخیص صحت کارت عابربانک
This file contains 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
The same thing implemented in
class-validator
. In the following piece of code I write a custom decorator to do the same thing:is-valid-card-number.decorator.ts
:bank-info.dto.ts
: