What is proper RegEx expression for SWIFT codes?
Asked Answered
L

3

14

I have to filter user input to on my web ASP.NET page:

<asp:TextBox runat="server" ID="recipientBankIDTextBox" MaxLength="11" />
<asp:RegularExpressionValidator runat="server" ValidationExpression="?" ControlToValidate="recipientBankIDTextBox" ErrorMessage="*" />

As far is I know SWIFT code must contain 5 or 6 letters and other symbols up to total length 11 are alphanumeric.

How to implement such rule properly?

Lila answered 12/6, 2010 at 9:27 Comment(0)
J
38

A swift code should be 8 or 11 letters or digits where the first six must be letters. But anyway it doesn't really matter what it is, what matters is that you understand how to create such an expression. Here is a regular expression with annotations to show you what the parts mean.

^[A-Z]{6}[A-Z0-9]{2}([A-Z0-9]{3})?$
       ^          ^           ^  ^
       |          |           |  |
       6 letters  2 letters   3 letters or digits
                  or digits      |
                                 last three are optional

All the examples on Wikipedia show only upper case letters A-Z. If you also want to allow lowercase letters then change A-Z to A-Za-z. I would check the ISO standard to see what that says, but unfortunately it's not free to obtain a copy.

Joellenjoelly answered 12/6, 2010 at 9:32 Comment(0)
D
6

According to http://en.wikipedia.org/wiki/ISO_9362 ...

/[A-Z]{6}[A-Z0-9]{2}([A-Z0-9]{3})?/i

The SWIFT code is 8 or 11 characters, made up of:

  • 4 letters: institution code or bank code.
  • 2 letters: ISO 3166-1 alpha-2 country code (exceptionally, SWIFT has assigned the code XK to Republic of Kosovo, which does not have an ISO 3166-1 country code)
  • 2 letters or digits: location code if the second character is "0", then it is typically a test BIC as opposed to a BIC used on the live network. if the second character is "1", then it denotes a passive participant in the SWIFT network if the second character is "2", then it typically indicates a reverse billing BIC, where the recipient pays for the message as opposed to the more usual mode whereby the sender pays for the message.
  • 3 letters or digits: branch code, optional ('XXX' for primary office)

Where an eight digit code is given, it may be assumed that it refers to the primary office.

Daloris answered 12/6, 2010 at 9:32 Comment(0)
D
6

This should do the trick

^([a-zA-Z]){4}([a-zA-Z]){2}([0-9a-zA-Z]){2}([0-9a-zA-Z]{3})?$
Dombrowski answered 12/6, 2010 at 9:33 Comment(2)
What for there are two different groups - first and second - with the equal expression? Why not just ([a-zA-Z]){6} ?Lila
Yes you are correct you could do that - I split it in to two groups purely on the basis that the first group belongs to the bank and the second group is the country code. e.g for Natwest Bank, UK. NWBK plus GB.Dombrowski

© 2022 - 2024 — McMap. All rights reserved.