# IBAN validator

## Recommended Posts

```Func _IbanValidate(\$iban)

;~  1. Parse string, but I don't check if IBAN length is correct as per the country, no need.
\$iban = StringRegExpReplace ( StringUpper(\$iban), "[^A-Z0-9]", "")

;~      2. Move the four initial characters to the end of the string
\$IBAN_iso = StringLeft(\$iban, 2) ; If need to know Country code
\$IBAN_key = StringMid(\$iban, 3, 2) ; next 2 digits
\$IBAN_bban= StringMid(\$iban, 5)
\$IBAN_Reverse = stringsplit(\$IBAN_bban & \$IBAN_iso & \$IBAN_key, "") ; complet reversed Iban to array

;~  3. Replace each letter in the string with two digits, thereby expanding the string, where A = 10, B = 11, ..., Z = 35
\$IBAN_Int = "" ; Iban integer
For \$i = 1 To \$IBAN_Reverse[0]
If StringIsDigit(\$IBAN_Reverse[\$i]) Then
\$IBAN_Int &= \$IBAN_Reverse[\$i]
Else
\$IBAN_Int &= Asc(\$IBAN_Reverse[\$i])-55
EndIf
Next

;~  4. Interpret the string as a decimal integer and compute the remainder of that number on division by 97
;~     Piece-wise calculation D mod 97
;~     Starting from the leftmost digit of D, construct a number using the first 9 digits and call it N.
;~     Calculate N mod 97. If the result is less than 10, prefix the result with a 0, giving a result in the range 00 to 96.
;~     Construct a new 9-digit N by concatenating above result (step 2) with the next 7 digits of D. If there are fewer than 7 digits remaining in D but at least one, then construct a new N, which will have less than 9 digits, from the above result (step 2) followed by the remaining digits of D
;~     Repeat steps 2–3 until all the digits of D have been processed
;~     The result of the final calculation in step 2 will be D mod 97 = N mod 97.

\$mod_N = Mod(StringLeft(\$IBAN_Int, 9),97)
\$mod_N = (\$mod_N < 10) ? "0" & \$mod_N : \$mod_N
\$mod_N = Mod(\$mod_N&StringMid(\$IBAN_Int, 10, 7),97)
\$mod_N = (\$mod_N < 10) ? "0" & \$mod_N : \$mod_N
\$mod_N = Mod(\$mod_N&StringMid(\$IBAN_Int, 17, 7),97)
\$mod_N = (\$mod_N < 10) ? "0" & \$mod_N : \$mod_N
\$IBAN_Validate = Mod(int(\$mod_N&StringMid(\$IBAN_Int, 24, 7)),97) == 1

Return \$IBAN_Validate
EndFunc

\$valide = _IbanValidate("GB82 WEST 1234 5698 7654 32")
ConsoleWrite(\$valide&@LF)```
Edited by Bess

##### Share on other sites

This is quite interesting

##### Share on other sites

I have to find my own verifier, and compare.

I did a similar feature about two years ago.

But at first glance it looks pretty good

mLipok

Signature beginning:   Wondering who uses AutoIT and what it can be used for ?
*
How to use IE.au3  UDF with  AutoIt v3.3.14.x  for other useful stuff click the following button

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind.

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
, be   and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2019-10-01

##### Share on other sites

It looks like a good start, but I did notice that ...

`\$IBAN_Int &= Int(dec(_StringToHex(\$IBAN_Reverse[\$i]))-55)`

can be reduced to using StringUpper(), Asc() and then minus 55. I would also add a check (if not done before) that the value is between 10 and 36.

##### Share on other sites

btw.

Welcome to the forum

Cheers

mLipok

Signature beginning:   Wondering who uses AutoIT and what it can be used for ?
*
How to use IE.au3  UDF with  AutoIt v3.3.14.x  for other useful stuff click the following button

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind.

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
, be   and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2019-10-01

##### Share on other sites

Sorry I couldn't help it.

```#include <StringConstants.au3>

Local \$UNITED_KINGDOM = 22, _
\$sIBAN = 'GB82 WEST 1234 5698 7654 32'
ConsoleWrite('IsIBAN: ' & _IsIBAN(\$sIBAN, \$UNITED_KINGDOM) & @CRLF)

Func _IsIBAN(\$sIBAN, \$iChars)
\$sIBAN = StringRegExpReplace(\$sIBAN, '[^A-Za-z0-9]', '')
ConsoleWrite('Formatted IBAN: ' & \$sIBAN & @CRLF)
Local \$iLength = UBound(StringRegExp(\$sIBAN, '([A-Z0-9])', \$STR_REGEXPARRAYGLOBALMATCH))
ConsoleWrite('Length: ' & \$iLength & @CRLF)
Local \$iRemainder = 0
If \$iLength = \$iChars Then
\$sIBAN = StringRegExpReplace(\$sIBAN, '^([A-Z0-9]{4})([A-Z0-9]{' & (\$iLength - 4) & '})', '\$2\$1')
Local \$sChr = '', \$sIBANToInt = ''
For \$i = 1 To \$iLength
\$sChr = StringMid(\$sIBAN, \$i, 1)
If Not StringIsInt(\$sChr) Then
\$sChr = Asc(\$sChr) - 55
EndIf
\$sIBANToInt &= \$sChr
Next
; Taken from some beautiful C# example: http://iban.saltug.net/
While (StringLen(\$sIBANToInt) >= 7)
\$iRemainder = Mod(Int(\$iRemainder & StringLeft(\$sIBANToInt, 7)), 97)
\$sIBANToInt = StringTrimLeft(\$sIBANToInt, 7)
WEnd
\$iRemainder = Mod(Int(\$iRemainder & \$sIBANToInt), 97)
EndIf
Return \$iRemainder = 1
EndFunc   ;==>_IsIBAN```

##### Share on other sites

so this would be a valid IBAN in both validation routines above

NL-62-DEUT-04485-93815

I am sure payment engines will reject above (similar to payment engines will refuse IBANS with spaces and even SEPA XML XSD's will not allow)

Some code related to validating (just mod 97 on long numbers)

```test()

func test()
consolewrite( 98 - ModLong(IBAN2Number("NL20INGB0001234567"), 97) & @CRLF)
consolewrite( 98 - ModLong(IBAN2Number("NL94ABNA0458379352"), 97)& @CRLF)
consolewrite( 98 - ModLong(IBAN2Number("NL 0458379352"), 97) & @CRLF)
consolewrite( 98 - ModLong(IBAN2Number("NL  ABNA0458379352"), 97)& @CRLF)
consolewrite( 98 - ModLong(IBAN2Number("NL  ABNA0000000555"), 97)& @CRLF)
EndFunc

func modLong(\$divident, \$divisor)
const \$partLength=6
while stringlen(\$divident) > \$partLength
\$part=stringleft(\$divident, \$partLength)
\$divident= string(mod(\$part,\$divisor)) & stringmid(\$divident,\$partlength+1)
WEnd
return mod(\$divident,\$divisor)
EndFunc

func IBAN2Number(\$IBAN)
local \$tStr, \$newStr
\$tStr=stringMid(\$IBAN, 5) & stringLeft(\$IBAN, 2)
for \$i=1 to stringlen(\$tStr)
\$tVal=asc(stringmid(\$tStr,\$i,1))
if \$tval>=65 and \$tval <=90 Then
\$newStr=\$newStr & string(\$tVal-55)
Else
\$newStr=\$newStr & stringmid(\$tStr,\$i,1)
EndIf
Next
return \$newStr & "00"
endFunc```

## Create an account

Register a new account

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
• Our Picks
×
• Create New...