insurello/elm-swedish-bank-account-number/1.0.2

NOTE: the latest version is 2.0.4

SwedishBankAccountNumber

This library lets you validate Swedish bank account numbers.

First, validate a clearing number using SwedishBankAccountNumber.ClearingNumber.fromString.

Then, validate an account number using SwedishBankAccountNumber.create.

Main

Represents a Swedish bank account number.

This is an example of a valid Swedish bank account number:

9420 - 4172385

A bank account number consists of two parts:

  1. The clearing number: 9420
  2. The account number: 4172385

The only way to create a value of the SwedishBankAccountNumber type is by calling create. This way, if you encounter a SwedishBankAccountNumber you always know that it’s valid.

SwedishBankAccountNumber also contains information about which Swedish bank the bank account number is for.

type Error
= BadAccountNumberLength Int
| BadChecksum

Trying to construct a SwedishBankAccountNumber can fail in two ways:

  • The account number is too short or too long. BadAccountNumberLength contains how many digits were actually passed; getAccountNumberLength lets you know how many are valid.
  • The checksum of the bank account number is invalid (the last digit of account numbers is a control digit that is supposed to make the checksum add up).

(Constructing a ClearingNumber can fail in its own ways – see SwedishBankAccountNumber.ClearingNumber.Error.)

create : ClearingNumber -> String -> Result Error SwedishBankAccountNumber

Validate and construct a SwedishBankAccountNumber.

First, you need to get a ClearingNumber via SwedishBankAccountNumber.ClearingNumber.fromString.

Then, pass the validated ClearingNumber and an unvalidated account number string to get a full bank account number.

The account number string is allowed to have any kind of crazy formatting. The function simply takes all the digits of the string and discards all other characters. The following all mean the same thing:

4172385
417 23 85
417,23,85
abc417-23#85!

If you have a form field for the account number, you might want to normalize it on blur. You can use String.filter Char.isDigit myString to do so.

-> { bankName : String
, clearingNumber : String
, accountNumber : String
}

When you need to display a SwedishBankAccountNumber or send it via HTTP to your backend, use this function to get all of its data.

  • bankName is a human readable string name of the bank identified from the clearing number, such as “Länsförsäkringar Bank”.
  • clearingNumber and accountNumber are strings containing digits only (no hyphens or spaces or anything), such as “9420“ and “4172385”, respectively.

Helpers

= FixedLength Int
| Range Int Int

Represents the length constraints of an account number.

The most common value is FixedLength 7, which all modern “type 1” accounts have.

Older “type 2” accounts usually have FixedLength 10.

Then there are some special cases:

  • Handelsbanken uses FixedLength 9.
  • Swedbank accounts with clearing numbers starting with 8 have 6-10 digits (Range 6 10).
  • Nordea Plusgirot use 7-10 digits (Range 7 10).

See https://github.com/jop-io/kontonummer.js/issues/6 for more information.

Get the length constraints of an account number. Since banks have different rules this depends on the clearing number.

Let’s say you have two form fields – one for the clearing number, and one for the account number. After the clearing number has been filled, you might want to show how long the account number is expected to be. Then this function will come in handy.

By the way – clearing numbers are always 4 digits, except Swedbank clearing numbers starting with 8 which are 5 digits.