Jump to content

IBAN: Difference between revisions

1,709 bytes added ,  7 years ago
Added Kotlin
(Added Kotlin)
Line 1,783:
"GB82 WEST 1234 5698 7654 32" | is_valid_iban #=> true
"GB82 TEST 1234 5698 7654 32" | is_valid_iban #=> false</lang>
 
=={{header|Kotlin}}==
<lang scala>// version 1.0.6
 
import java.math.BigInteger
 
object Iban {
/* List updated to release 73, January 2017, of IBAN Registry (75 countries) */
private val countryCodes =
"AD24 AE23 AL28 AT20 AZ28 BA20 BE16 BG22 BH22 BR29 BY28 CH21 CR22 CY28 CZ24 DE22 " +
"DK18 DO28 EE20 ES24 FI18 FO18 FR27 GB22 GE22 GI23 GL18 GR27 GT28 HR21 HU28 IE22 " +
"IL23 IQ23 IS26 IT27 JO30 KW30 KZ20 LB28 LC32 LI21 LT20 LU20 LV21 MC27 MD24 ME22 " +
"MK19 MR27 MT31 MU30 NL18 NO15 PK24 PL28 PS29 PT25 QA29 RO24 RS22 SA24 SC31 SE24 " +
"SI19 SK24 SM27 ST25 SV28 TL23 TN24 TR26 UA29 VG24 XK20"
 
private fun checkCountryCode(cc: String) = cc in countryCodes
 
fun validate(iban: String): Boolean {
// remove spaces from IBAN
var s = iban.replace(" ", "")
 
// check country code
if (!checkCountryCode(s.substring(0, 2) + s.length)) return false
// move first 4 characters to end
s = s.substring(4) + s.substring(0, 4)
// replace A to Z with numbers 10 To 35
for (ch in 'A'..'Z') s = s.replace(ch.toString(), (ch - 55).toInt().toString())
// check whether mod 97 calculation gives a remainder of 1
return BigInteger(s) % BigInteger.valueOf(97L) == BigInteger.ONE
}
}
 
fun main(args: Array<String>) {
val ibans = arrayOf("GB82 WEST 1234 5698 7654 32", "GB82 TEST 1234 5698 7654 32")
for (iban in ibans) {
val isValid = Iban.validate(iban)
println(iban + if(isValid) " may be valid" else " is not valid")
}
}</lang>
 
{{out}}
<pre>
GB82 WEST 1234 5698 7654 32 may be valid
GB82 TEST 1234 5698 7654 32 is not valid
</pre>
 
=={{header|Logtalk}}==
9,490

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.