11

I need to check an input field for a German IBAN. The user should be allowed to leave in white spaces and input should be validated to have a starting DE and then exact 20 characters numbers and letters.

Without the white space allowance, I tried

^[DE]{2}([0-9a-zA-Z]{20})$

but I cannot find where and how I can add "white spaces anywhere allowed.

This should be simple, but I simply cannot find a solution.

Thanks for help!

Jezen Thomas
  • 13,004
  • 5
  • 49
  • 87
viddity
  • 111
  • 1
  • 1
  • 4
  • 1
    What language are you using? Is removing all whitespaces from the string a suitable solution? Also, FYI, `[DE]{2}` matches `DD` or `ED`: see http://www.regular-expressions.info/charclass.html – Robin May 05 '14 at 11:45
  • 1
    as I'm saying in my answer, you're not using the right tool for the task. You *should not* check something as important as an IBAN against a format, but check the validity of the whole code by calculating the checksum, which is, agorithmically, as complex as a regex check. – zmo May 05 '14 at 12:23
  • Tanks a lot! we do validate the correct IBAN format properly in the backend, but want to guide the user frontend-wise already too. – viddity May 05 '14 at 12:39
  • 1
    do not tend to just guide, just validate it. To repeat myself: a checksum calcul is faster, lighter and easier to do than a regex, and it's the best way to validate an IBAN input! – zmo May 08 '14 at 17:34

7 Answers7

28

Because you should use the right tool for the right task: you should not rely on regexps to validate IBAN numbers, but instead use the IBAN checksum algorithm to check the whole code is actually correct, making any regexp superfluous and redundant. i.e.: remove all spaces, rearrange the code, convert to integers, and compute remainder, here it's best explained.

Though, there am I trying to answer your question, for the fun of it:

what about:

^DE([0-9a-zA-Z]\s?){20}$

which only difference is allowing a whitespace (or not) after each occurence of a alphanumeric character.

here is the visualization:

Regular expression visualization

edit: for the OP's information, the only difference is that this regexp, from @ulugbex-umirov: (?:\s*[0-9a-zA-Z]\s*) does a lookahead check to see if there's a space between the iso country code and the checksum (which only made of numerical digits), which I do not support on purpose.

And actually to support a correct IBAN syntax, which is formed of groups of 4 characters, as the wikipedia page says:

^DE\d{2}\s?([0-9a-zA-Z]{4}\s?){4}[0-9a-zA-Z]{2}$

Regular expression visualization

example

If your UI is in Javascript, you can use that library for doing IBAN validation:

<script src="iban.js"></script>
<script>
    // the API is now accessible from the window.IBAN global object
    IBAN.isValid('hello world'); // false
    IBAN.isValid('BE68539007547034'); // true
</script>

so you know this is a valid IBAN, and can validate it before the data is ever even sent to the backend. Simpler, lighter and more elegant… Why do something else?

zmo
  • 22,917
  • 4
  • 48
  • 82
  • Good point about not relying on regexs to validate an IBAN, but I think a regex can be used in the frontend to catch any obvious mistakes and guide the user. I would do the more serious IBAN validation in the backend. – Jezen Thomas May 05 '14 at 12:38
  • thanks a ton for you quick and very helpful answer! we do validate in the backend as well. the regex is additional, for user guidance – viddity May 05 '14 at 12:40
  • nope, you're wrong, the checksum validation is really light, if not lighter than a regex check! And it would enable to help the user detect any *real* error, and not simply format errors, the format errors being often a problem. And also, you may support *any* checksum, and not only the german ones… and I've been customer to many german sites that only accept payments from german-speaking countries, whereas I'm not living in one of those… We're in Europe, we got SEPA and IBANs! :-) – zmo May 05 '14 at 12:41
  • 1
    btw, if your UI is in javascript, there's a bunch of JS libraries: [here](http://toms-cafe.de/iban/iban.html), [there](https://gist.github.com/krypton/5540925) or [there](https://github.com/arhs/iban.js) that impements the checksum calculation. – zmo May 05 '14 at 12:50
  • I came across this question sepcifically because I had to solve a bug where an old-style, national account number had by pure chance matched the checksum algorithm and thus passed as an IBAN. So I decided to add a regex check. Best solution: Do both. – fbitterlich Aug 26 '14 at 09:59
  • 1
    @fbitterlich did you post detailed information about this bug anywhere? I don't see it as an issue on the arhs/iban.js github page. – kluka Jan 07 '15 at 13:26
  • @klyonrad To clarify: the bug I mentioned was in my own (non-published) code. Adding a dual check (checksum and regex) allows me to more reliably tell IBAN and old-style account numbers apart. – fbitterlich Jan 08 '15 at 16:04
  • 1
    @zmo: This answer makes me want to be able to upvote twice :-) – Andresch Serj Oct 21 '15 at 06:58
  • Thank you, glad it helped :-) – zmo Oct 22 '15 at 05:59
  • There is even a [python library](https://github.com/mdomke/schwifty) for this. – MertTheGreat Jun 03 '20 at 12:34
22

Here is a list of IBANs from 70 Countries. I generated it with a python script i wrote based on this https://en.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]{4}\s?){1}([0-9]{1})([a-zA-Z0-9]{3}\s?)([a-zA-Z0-9]{4}\s?){2}([a-zA-Z0-9]{1})\s?
TN[a-zA-Z0-9]{2}\s?([0-9]{4}\s?){5}\s?
TR[a-zA-Z0-9]{2}\s?([0-9]{4}\s?){1}([0-9]{1})([a-zA-Z0-9]{3}\s?)([a-zA-Z0-9]{4}\s?){3}([a-zA-Z0-9]{2})\s?
AE[a-zA-Z0-9]{2}\s?([0-9]{3})([0-9]{1}\s?)([0-9]{4}\s?){3}([0-9]{3})\s?
GB[a-zA-Z0-9]{2}\s?([a-zA-Z]{4}\s?){1}([0-9]{4}\s?){3}([0-9]{2})\s?
VA[a-zA-Z0-9]{2}\s?([0-9]{3})([0-9]{1}\s?)([0-9]{4}\s?){3}([0-9]{2})\s?
VG[a-zA-Z0-9]{2}\s?([a-zA-Z0-9]{4}\s?){1}([0-9]{4}\s?){4}\s? 
Pascal Zoleko
  • 446
  • 4
  • 6
  • Don't know why the downvotes. This list is at least incredibly helpful. These seem correct. And the German regex is in your answer. – Bob van den Berg Oct 25 '19 at 12:59
6

Original:

^[DE]{2}([0-9a-zA-Z]{20})$

Regular expression visualization

Debuggex Demo

Modified:

^DE(?:\s*[0-9a-zA-Z]\s*){20}$

Regular expression visualization

Debuggex Demo

Ulugbek Umirov
  • 12,231
  • 2
  • 20
  • 29
4

This is the correct regex to match DE IBAN account numbers:

DE\d{2}[ ]\d{4}[ ]\d{4}[ ]\d{4}[ ]\d{4}[ ]\d{2}|DE\d{20}

Pass:   DE89 3704 0044 0532 0130 00|||DE89370400440532013000
Fail:   DE89-3704-0044-0532-0130-00
Pedro Lobito
  • 75,541
  • 25
  • 200
  • 222
3

Most simple solution I can think of:

^DE(\s*[[:alnum:]]){20}\s*$

In particular, your initial [DE]{2} is wrong, as it allows 'DD', 'EE', 'ED' as well as the intended 'DE'.

Jongware
  • 21,058
  • 8
  • 43
  • 86
1

To allow any amount of spaces anywhere:

^ *D *E( *[A-Za-z0-9]){20} *$

As you want to allow lower letters, also DE might be lower?

^ *[Dd] *[Ee]( *[A-Za-z0-9]){20} *$
  • ^ matches the start of the string
  • $ end anchor
  • in between each characters there are optional spaces *
  • [character class] defines a set/range of characters

To allow at most one space in between each characters, replace the quantifier * (any amount of) with ? (0 or 1). If supported, \s shorthand can be used to match [ \t\r\n\f] instead of space only.

Test on regex101.com, also see the SO regex FAQ

Community
  • 1
  • 1
Jonny 5
  • 11,051
  • 2
  • 20
  • 42
0

Using Google Apps Script, I pasted Laurent's code from github into a script and added the following code to test.

// Use the Apps Script IDE's "Run" menu to execute this code.
// Then look at the View > Logs menu to see execution results.

function myFunction() {
//https://github.com/arhs/iban.js/blob/master/README.md
// var IBAN = require('iban');
var t1 = IBAN.isValid('hello world'); // false
var t2 = IBAN.isValid('BE68539007547034'); // true
var t3 = IBAN.isValid('BE68 5390 0754 7034'); // true

Logger.log("Test 1 = %s", t1);
Logger.log("Test 2 = %s", t2);
Logger.log("Test 3 = %s", t3);
}

The only thing needed to run the example code was commenting out the require('iban') line: // var IBAN = require('iban'); Finally, instead of using client handlers to attempt a RegEx validation of IBAN input, I use a a server handler to do the validation.

Steven.AA
  • 79
  • 4