views:

108

answers:

3

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? TIO

+1  A: 

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

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

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.

Mark Byers
+1  A: 

This should do the trick

^([a-zA-Z]){4}([a-zA-Z]){2}([0-9a-zA-Z]){2}([0-9a-zA-Z]{3})?$
Barry
What for there are two different groups - first and second - with the equal expression? Why not just `([a-zA-Z]){6}` ?
abatishchev
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`.
Barry