delphistring-formattingdelphi-xe3editcontroliban

How to edit an IBAN number in four character groups?


https://en.wikipedia.org/wiki/International_Bank_Account_Number#Practicalities

The IBAN should not contain spaces when transmitted electronically: when printed it is expressed in groups of four characters separated by a single space, the last group being of variable length as shown in the example below:

A typical IBAN looks like this: GR16 0110 1250 0000 0001 2300 695 (taken from the above link).

I want to make it easier for users to enter IBAN numbers. Currently I use a TDBEdit to display the IBAN number and it is stored as characters (without the spaces) in the database.

I know that it is possible to format numbers using TNumericField.DisplayFormat, also there is TMaskEdit, but both aren't terribly useful for this purpose as IBAN is not a number and has different lenghts in different countries.

How to edit an IBAN number in four character groups in a DB control?

PS: I'm not asking for the actual IBAN validation as I already have that figured out.


Solution

  • You can use the EditMask property of the IBAN field as this will also work for string fields. A suitable EditMask for IBAN may look like this:

    ">LL00 aaaa aaaa aaaa aaaa aaaa aaaa aaaa aa;0; "
    

    The first character makes the edit field convert all characters to upper case. The next four entries require two alpha characters followed by two numeric ones. The blanks represent the requested gaps. The lower "a" allows an alphanumeric character but doesn't require it.

    The "0" in the second part of the mask will strip any literals (the gap blanks) from the entry stored in the field.

    The blank in the third part of the mask makes the gap blanks being displayed as blank.