How to validate SWIFT or BIC Codes using PHP and Regular Expressions
BIC and SWIFT codes have a pretty simple format, and are therefore not that difficult to validate with PHP using regular expressions. Indeed, before we look into the regular expression itself, let us have a look at the build-up of a SWIFT/BIC Code. We will use the Swiss bank UBS' Code as an example: UBSWCHZH80A.
UBSW is the bank code consisting of 4 Alphabetic Symbols.
CH is the country, in this case Switzerland, made up out of 2 Alphabetic Symbols.
ZH is the area code, in this case Z|rich. This part of the code consists of two alphanumeric symbols. If the second symbol is a 1, the participant is passive. If it is a 0 we are dealing with a test-BIC.
80A is the 3 symbol branch code, which also consists of alphanumeric symbols and is optional.
Add all this information up and we end up with the following regular expression:
^([a-zA-Z]){4}([a-zA-Z]){2}([0-9a-zA-Z]){2}([0-9a-zA-Z]{3})?$
And here is an example of a validation code being tested on various real BIC and SWIFT codes, and a few dummy ones:
SWIFT code is not valid.";
}
else
{
return "SWIFT code is valid.";
}
}
echo "SWIFT-BIC Natwest Offshore Bank - GB: RBOSGGSX
";
echo 'swift_validate("RBOSGGSX") -> '.swift_validate("RBOSGGSX")."
";
echo "SWIFT-BIC Raiffeisenbank Kitzb|hel - Austria: RZTIAT22263
";
echo 'swift_validate("RZTIAT22263") -> '.swift_validate("RZTIAT22263")."
";
echo "SWIFT-BIC Banque et Caisse d'Epargne de l'Etat - Luxemburg: BCEELULL
";
echo 'swift_validate("BCEELULL") -> '.swift_validate("BCEELULL")."
";
echo "SWIFT-BIC Deutschen Bundesbank - Germany: MARKDEFF
";
echo 'swift_validate("MARKDEFF") -> '.swift_validate("MARKDEFF")."
";
echo "SWIFT-BIC Volksbank Jever - Germany: GENODEF1JEV
";
echo 'swift_validate("GENODEF1JEV") -> '.swift_validate("GENODEF1JEV")."
";
echo "SWIFT-BIC UBS AG - Switzerland: UBSWCHZH80A
";
echo 'swift_validate("UBSWCHZH80A") -> '.swift_validate("UBSWCHZH80A")."
";
echo "SWIFT-BIC Clearstream Banking - S.A., Luxembourg: CEDELULLXXX
";
echo 'swift_validate("CEDELULLXXX") -> '.swift_validate("CEDELULLXXX")."
";
echo "Invalid Dummy Bank 1: CE1EL2LLFFF
";
echo 'swift_validate("CE1EL2LLFFF") -> '.swift_validate("CE1EL2LLFFF")."
";
echo "Invalid Dummy Bank 2: E31DCLLFFF
";
echo 'swift_validate("E31DCLLFFF") -> '.swift_validate("E31DCLLFFF")."
";
?>