I wrote a regex for php function pregmatch which is like this:
^([a-zA-Z]){4}([a-zA-Z]){2}([0-9a-zA-Z]){2}([0-9a-zA-Z]{3})?$^
Now I need to check the consistency of an BIC string.
Something is wrong with it... it is always correct. And I have no idea why.
The code I use is something like this:
/**
* Checks the correct format from the
* @param string $bic
* @return boolean
*/
public function checkBic($bic)
{
$bic = $this->cleanFromSeparators($bic);
if (preg_match($this->getBicCompare(), $bic)) {
return true;
} else {
return false;
}
}
private function getBicCompare()
{
return "^([a-zA-Z]){4}([a-zA-Z]){2}([0-9a-zA-Z]){2}([0-9a-zA-Z]{3})?$^";
}
EDIT:
Here are some references for BIC format from the swift account:
http://www.sage.co.uk/sage1000v2_1/form_help/workingw/subfiles/iban_and_bic.htm
http://en.wikipedia.org/wiki/ISO_9362
http://www.swift.com/products_services/bic_and_iban_format_registration_bic_details?rdct=t
an example BIC would be:
NOLADE21STS
OPSKATWW
The regex should only return true if the string consists of the following code: its length is eight or eleven characters and that consists of:
Bank code - 4 alphabetic characters Country code - 2 letters Location code - 2 alphanumeric characters, except zero Branch code - 3 alphanumeric characters
These are the specifications.
So the length can be either 11 or 8, first 4 can be anything, then 2 letters is a must, then 2 numbers and optional 3 alphanumeric.
The following are not valid:
abcdefxx
abcdefxxyyy
These also are not valid:
aaaa11xx
aaaa11xxyyy
and so on.
Format of a SWIFT/BIC number. Bank code A-Z 4 letters representing the bank. It usually looks like a shortened version of that bank's name. Country code A-Z 2 letters representing the country the bank is in. Location code 0-9 A-Z 2 characters made up of letters or numbers.
A SWIFT (Society for Worldwide Interbank Financial Telecommunications) code is an 8-11 character long, standard format code that identifies banks and financial institutions worldwide.
A SWIFT code consists of 8 or 11 characters, where each section of characters describe one part of a bank's location. The codes are formatted in the “AAAABBCCDDD”, where: AAAA: Bank code. BB: Country code.
You are using ^
as delimiter? You probably want something more like:
'/^[a-z]{6}[0-9a-z]{2}([0-9a-z]{3})?\z/i'
Structure
The latest edition is ISO 9362:2009 (dated 2009-10-01). 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
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)
(http://en.wikipedia.org/wiki/ISO_9362)
(different definition in German-Wiki http://de.wikipedia.org/wiki/ISO_9362)
2 letters or digits: location code The first character must not be the digit "0" or "1". The letter 'O' is not allowed as a second character. (Regex for this definition: [2-9a-z][0-9a-np-z])
'/^[a-z]{6}[2-9a-z][0-9a-np-z]([a-z0-9]{3}|x{3})?$/i'
This is the official SEPA pattern for validating BIC
[A-Z]{6,6}[A-Z2-9][A-NP-Z0-9]([A-Z0-9]{3,3}){0,1}
I think this one would do:
/^[a-z0-9]{4}[a-z]{2}\d{2}([a-z0-9]{3})?$/
That is:
^
[a-z0-9]{4}
\d{2}
?
suffix) alphanumerich chars, ([a-z0-9]{3})?
$
You can see it in action and test it here (I used your samples).
Anyway, from the rules you are reporting, OPSKATWW
shouldn't be a valid BIC since it has no numbers after the first 6 letters.
echo preg_match('/^[A-Z]{4}(AC|AD|AE|AF|AG|AI|AL|AM|AN|AO|AQ|AR|AS|AT|AU|AW|AX|AZ|BA|BB|BD|BE|BF|BG|BH|BI|BJ|BL|BM|BN|BO|BQ|BR|BS|BT|BV|BW|BY|BZ|CA|CC|CD|CE|CF|CG|CH|CI|CK|CL|CM|CN|CO|CP|CR|CS|CU|CV|CW|CX|CY|CZ|DD|DE|DG|DJ|DK|DM|DO|DZ|EA|EC|EE|EG|EH|ER|ES|ET|EU|FI|FJ|FK|FM|FO|FR|FX|GA|GB|GD|GE|GF|GG|GH|GI|GL|GM|GN|GP|GQ|GR|GS|GT|GU|GW|GY|HK|HM|HN|HR|HT|HU|IC|ID|IE|IL|IM|IN|IO|IQ|IR|IS|IT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F|MG|MH|MK|ML|MM|MN|MO|MP|MQ|MR|MS|MT|MU|MV|MW|MX|MY|MZ|NA|NC|NE|NF|NG|NI|NL|NO|NP|NR|NT|NU|NZ|OM|PA|PE|PF|PG|PH|PK|PL|PM|PN|PR|PS|PT|PW|PY|QA|RE|RO|RS|RU|RW|SA|SB|SC|SD|SE|SF|SG|SH|SI|SJ|SK|SL|SM|SN|SO|SR|SS|ST|SU|SV|SX|SY|SZ|TA|TC|TD|TF|TG|TH|TJ|TK|TL |TM|TN|TO|TP|TR|TT|TV|TW|TZ|UA|UG|UK|UM|US|UY|UZ|VA|VC|VE|VG|VI|VN|VU|WF|WS|XK|YE|YT|ZA|ZM|ZR|ZW)[2-9A-Z][0-9A-NP-Z](XXX|[0-9A-WYZ][0-9A-Z]{2})?$/i
Also this is content validation and not just format validation. But it will save you a bit of code :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With