-
-
Save mhf-ir/c17374fae395a57c9f8e5fe7a92bbf23 to your computer and use it in GitHub Desktop.
function iso7064Mod97_10(iban) { | |
var remainder = iban, | |
block; | |
while (remainder.length > 2){ | |
block = remainder.slice(0, 9); | |
remainder = parseInt(block, 10) % 97 + remainder.slice(block.length); | |
} | |
return parseInt(remainder, 10) % 97; | |
} | |
function validateIranianSheba(str) { | |
var pattern = /IR[0-9]{24}/; | |
if (str.length !== 26) { | |
return false; | |
} | |
if (!pattern.test(str)) { | |
return false; | |
} | |
var newStr = str.substr(4); | |
var d1 = str.charCodeAt(0) - 65 + 10; | |
var d2 = str.charCodeAt(1) - 65 + 10; | |
newStr += d1.toString() + d2.toString() + str.substr(2, 2); | |
var remainder = iso7064Mod97_10(newStr); | |
if (remainder !== 1) { | |
return false; | |
} | |
return true; | |
}; | |
console.log(validateIranianSheba('ENTER SHEBA NUMBER HERE IT\'SRETURN TRUE OR FALSE')); |
Here is the
typescript
version:export function iso7064Mod97_10(iban: string): number { let remainder: string = iban; let block: string; while (remainder.length > 2) { block = remainder.slice(0, 9); remainder = parseInt(block, 10) % 97 + remainder.slice(block.length); } return parseInt(remainder, 10) % 97; } export function validateIranianSheba(iban: string): boolean { if (!iban.startsWith('IR')) { iban = 'IR' + iban; } const pattern = /IR[0-9]{24}/; if (iban.length !== 26) { return false; } if (!pattern.test(iban)) { return false; } let testString = iban.substring(4); const d1 = iban.charCodeAt(0) - 65 + 10; const d2 = iban.charCodeAt(1) - 65 + 10; testString += d1.toString() + d2.toString() + iban.substring(2, 2); if (iso7064Mod97_10(testString) !== 1) { return false; } return true; }
the part you are creating test string at iban.substring(2, 2);
is wrong! you should switch it to iban.substring(2, 4)
. the way the substr
works is a little different than substring. substring expect the end in the second argument not length!
Here is the
typescript
version:export function iso7064Mod97_10(iban: string): number { let remainder: string = iban; let block: string; while (remainder.length > 2) { block = remainder.slice(0, 9); remainder = parseInt(block, 10) % 97 + remainder.slice(block.length); } return parseInt(remainder, 10) % 97; } export function validateIranianSheba(iban: string): boolean { if (!iban.startsWith('IR')) { iban = 'IR' + iban; } const pattern = /IR[0-9]{24}/; if (iban.length !== 26) { return false; } if (!pattern.test(iban)) { return false; } let testString = iban.substring(4); const d1 = iban.charCodeAt(0) - 65 + 10; const d2 = iban.charCodeAt(1) - 65 + 10; testString += d1.toString() + d2.toString() + iban.substring(2, 2); if (iso7064Mod97_10(testString) !== 1) { return false; } return true; }the part you are creating test string at
iban.substring(2, 2);
is wrong! you should switch it toiban.substring(2, 4)
. the way thesubstr
works is a little different than substring. substring expect the end in the second argument not length!
Thanks for your contribution. I fixed that problem.
Great job
@hasanparasteh
The last statement in the validateIranianSheba function could also be simplified to:
return iso7064Mod97_10(testString) !== 1
here is the python
version
import re
def iso7064_mod97_10(iban_numeric: str) -> int:
while len(iban_numeric) > 2:
block = iban_numeric[:9]
iban_numeric = str(int(block) % 97) + iban_numeric[len(block):]
return int(iban_numeric) % 97
def validate_iranian_sheba(sheba: str) -> bool:
if not re.fullmatch(r'IR\d{24}', sheba):
return False
rearranged = sheba[4:] + str(ord(sheba[0]) - 55) + str(ord(sheba[1]) - 55) + sheba[2:4]
return iso7064_mod97_10(rearranged) == 1
# Example usage
if __name__ == "__main__":
test_sheba1 = "IR820540102680020817909002"
test_sheba2 = "IR320540102680020817909002"
print("Correct Sheba:", validate_iranian_sheba(test_sheba1))
print("Wrong Sheba:", validate_iranian_sheba(test_sheba2))
class-validator
deocrator: