Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
گولنگ و پی اچ پی اعتبار سنجی کارت عابر بانک ایران در جاوا اسکریپت , Iranian bank cards validator function, Javascript Golang php ,تابع تشخیص صحت کارت عابربانک
"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);
}
@parsa-JPM
Copy link

parsa-JPM commented Feb 15, 2021

خیلی خوب بود اینم تبدیلش به جاوا

    private boolean isValidCardNumber(String cardNumber) {

        if (cardNumber == null || cardNumber.length() != 16) {
            return false;
        }

        int cardTotal = 0;
        for (int i = 0; i < 16; i += 1) {
            int c = Character.getNumericValue(cardNumber.charAt(i));

            if (i % 2 == 0) {
                cardTotal += ((c * 2 > 9) ? (c * 2) - 9 : (c * 2));
            } else {
                cardTotal += c;
            }
        }

        return (cardTotal % 10 == 0);
    }

Loading

@behnampro
Copy link

behnampro commented Feb 15, 2021

اعتبار سنجی کارت عابر بانک ایران در گولنگ \ Validation function of Iranian bank cards in golang \ تابع تشخیص صحت کارت عابربانک

func CheckCardNumber(cardNumberStr string) bool {
	if len(cardNumberStr) != 16 {
		return false
	}
	var cardTotal int64 = 0
	for i, ch := range cardNumberStr {
		c, err := strconv.ParseInt(string(ch),10,8)
		if err != nil {
			return false
		}
		if i % 2 == 0 {
			if c * 2 > 9 {
				cardTotal = cardTotal + (c * 2)- 9
			} else {
				cardTotal = cardTotal + (c * 2)
			}
		} else {
			cardTotal += c
		}
	}

	return cardTotal % 10 == 0
}

Loading

@PlusMa
Copy link

PlusMa commented Mar 29, 2021

و این هم تبدیلش به PHP

function validateCardNumber($cardNumber) {
    if(empty($cardNumber) || strlen($cardNumber) !== 16) {
        return false;
    }
    $cardToArr = str_split($cardNumber);
    $cardTotal = 0;
    for($i = 0; $i<16; $i++) {
        $c = (int)$cardToArr[$i];
        if($i % 2 === 0) {
            $cardTotal += (($c * 2 > 9) ? ($c * 2) - 9 : ($c * 2));
        } else {
            $cardTotal += $c;
        }
    }
    return ($cardTotal % 10 === 0);
}

Loading

@irmaster
Copy link

irmaster commented Nov 26, 2021

python

def card_validate(value):
    if not len(value) == 16:
        raise "The card did not validate"

    items = []
    for i in range(0, len(value)):
        k = ((i + 1) % 2) + 1
        r = k * int(value[i])
        items.append(r - 9 if r >= 10 else r)

    if sum(items) % 10 != 0:
        raise "The card did not validate"

Loading

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