Split out iban and bic

This commit is contained in:
Nick Schonning
2013-03-22 20:18:25 -04:00
committed by Jörn Zaefferer
parent e1ba7f0b18
commit eef45bcc72
3 changed files with 138 additions and 140 deletions

View File

@@ -33,146 +33,6 @@
}()); }());
/**
* IBAN is the international bank account number.
* It has a country - specific format, that is checked here too
*/
jQuery.validator.addMethod("iban", function(value, element) {
// some quick simple tests to prevent needless work
if (this.optional(element)) {
return true;
}
if (!(/^([a-zA-Z0-9]{4} ){2,8}[a-zA-Z0-9]{1,4}|[a-zA-Z0-9]{12,34}$/.test(value))) {
return false;
}
// check the country code and find the country specific format
var iban = value.replace(/ /g,'').toUpperCase(); // remove spaces and to upper case
var countrycode = iban.substring(0,2);
var bbancountrypatterns = {
'AL': "\\d{8}[\\dA-Z]{16}",
'AD': "\\d{8}[\\dA-Z]{12}",
'AT': "\\d{16}",
'AZ': "[\\dA-Z]{4}\\d{20}",
'BE': "\\d{12}",
'BH': "[A-Z]{4}[\\dA-Z]{14}",
'BA': "\\d{16}",
'BR': "\\d{23}[A-Z][\\dA-Z]",
'BG': "[A-Z]{4}\\d{6}[\\dA-Z]{8}",
'CR': "\\d{17}",
'HR': "\\d{17}",
'CY': "\\d{8}[\\dA-Z]{16}",
'CZ': "\\d{20}",
'DK': "\\d{14}",
'DO': "[A-Z]{4}\\d{20}",
'EE': "\\d{16}",
'FO': "\\d{14}",
'FI': "\\d{14}",
'FR': "\\d{10}[\\dA-Z]{11}\\d{2}",
'GE': "[\\dA-Z]{2}\\d{16}",
'DE': "\\d{18}",
'GI': "[A-Z]{4}[\\dA-Z]{15}",
'GR': "\\d{7}[\\dA-Z]{16}",
'GL': "\\d{14}",
'GT': "[\\dA-Z]{4}[\\dA-Z]{20}",
'HU': "\\d{24}",
'IS': "\\d{22}",
'IE': "[\\dA-Z]{4}\\d{14}",
'IL': "\\d{19}",
'IT': "[A-Z]\\d{10}[\\dA-Z]{12}",
'KZ': "\\d{3}[\\dA-Z]{13}",
'KW': "[A-Z]{4}[\\dA-Z]{22}",
'LV': "[A-Z]{4}[\\dA-Z]{13}",
'LB': "\\d{4}[\\dA-Z]{20}",
'LI': "\\d{5}[\\dA-Z]{12}",
'LT': "\\d{16}",
'LU': "\\d{3}[\\dA-Z]{13}",
'MK': "\\d{3}[\\dA-Z]{10}\\d{2}",
'MT': "[A-Z]{4}\\d{5}[\\dA-Z]{18}",
'MR': "\\d{23}",
'MU': "[A-Z]{4}\\d{19}[A-Z]{3}",
'MC': "\\d{10}[\\dA-Z]{11}\\d{2}",
'MD': "[\\dA-Z]{2}\\d{18}",
'ME': "\\d{18}",
'NL': "[A-Z]{4}\\d{10}",
'NO': "\\d{11}",
'PK': "[\\dA-Z]{4}\\d{16}",
'PS': "[\\dA-Z]{4}\\d{21}",
'PL': "\\d{24}",
'PT': "\\d{21}",
'RO': "[A-Z]{4}[\\dA-Z]{16}",
'SM': "[A-Z]\\d{10}[\\dA-Z]{12}",
'SA': "\\d{2}[\\dA-Z]{18}",
'RS': "\\d{18}",
'SK': "\\d{20}",
'SI': "\\d{15}",
'ES': "\\d{20}",
'SE': "\\d{20}",
'CH': "\\d{5}[\\dA-Z]{12}",
'TN': "\\d{20}",
'TR': "\\d{5}[\\dA-Z]{17}",
'AE': "\\d{3}\\d{16}",
'GB': "[A-Z]{4}\\d{14}",
'VG': "[\\dA-Z]{4}\\d{16}"
};
var bbanpattern = bbancountrypatterns[countrycode];
// As new countries will start using IBAN in the
// future, we only check if the countrycode is known.
// This prevents false negatives, while almost all
// false positives introduced by this, will be caught
// by the checksum validation below anyway.
// Strict checking should return FALSE for unknown
// countries.
if (typeof bbanpattern !== 'undefined') {
var ibanregexp = new RegExp("^[A-Z]{2}\\d{2}" + bbanpattern + "$", "");
if (!(ibanregexp.test(iban))) {
return false; // invalid country specific format
}
}
// now check the checksum, first convert to digits
var ibancheck = iban.substring(4,iban.length) + iban.substring(0,4);
var ibancheckdigits = "";
var leadingZeroes = true;
var charAt;
for (var i =0; i<ibancheck.length; i++) {
charAt = ibancheck.charAt(i);
if (charAt !== "0") {
leadingZeroes = false;
}
if (!leadingZeroes) {
ibancheckdigits += "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".indexOf(charAt);
}
}
// calculate the result of: ibancheckdigits % 97
var cRest = '';
var cOperator = '';
for (var p=0; p<ibancheckdigits.length; p++) {
var cChar = ibancheckdigits.charAt(p);
cOperator = '' + cRest + '' + cChar;
cRest = cOperator % 97;
}
return cRest === 1;
}, "Please specify a valid IBAN");
/**
* BIC is the business identifier code (ISO 9362). This BIC check is not a guarantee for authenticity.
*
* BIC pattern: BBBBCCLLbbb (8 or 11 characters long; bbb is optional)
*
* BIC definition in detail:
* - First 4 characters - bank code (only letters)
* - Next 2 characters - ISO 3166-1 alpha-2 country code (only letters)
* - Next 2 characters - location code (letters and digits)
* a. shall not start with '0' or '1'
* b. second character must be a letter ('O' is not allowed) or one of the following digits ('0' for test (therefore not allowed), '1' for passive participant and '2' for active participant)
* - Last 3 characters - branch code, optional (shall not start with 'X' except in case of 'XXX' for primary office) (letters and digits)
*/
jQuery.validator.addMethod("bic", function(value, element) {
return this.optional( element ) || /^([A-Z]{6}[A-Z2-9][A-NP-Z1-2])(X{3}|[A-WY-Z0-9][A-Z0-9]{2})?$/.test( value );
}, "Please specify a valid BIC code");
jQuery.validator.addMethod("dateNL", function(value, element) { jQuery.validator.addMethod("dateNL", function(value, element) {
return this.optional(element) || /^(0?[1-9]|[12]\d|3[01])[\.\/\-](0?[1-9]|1[012])[\.\/\-]([12]\d)?(\d\d)$/.test(value); return this.optional(element) || /^(0?[1-9]|[12]\d|3[01])[\.\/\-](0?[1-9]|1[012])[\.\/\-]([12]\d)?(\d\d)$/.test(value);
}, "Please enter a correct date"); }, "Please enter a correct date");

16
src/additional/bic.js Normal file
View File

@@ -0,0 +1,16 @@
/**
* BIC is the business identifier code (ISO 9362). This BIC check is not a guarantee for authenticity.
*
* BIC pattern: BBBBCCLLbbb (8 or 11 characters long; bbb is optional)
*
* BIC definition in detail:
* - First 4 characters - bank code (only letters)
* - Next 2 characters - ISO 3166-1 alpha-2 country code (only letters)
* - Next 2 characters - location code (letters and digits)
* a. shall not start with '0' or '1'
* b. second character must be a letter ('O' is not allowed) or one of the following digits ('0' for test (therefore not allowed), '1' for passive participant and '2' for active participant)
* - Last 3 characters - branch code, optional (shall not start with 'X' except in case of 'XXX' for primary office) (letters and digits)
*/
jQuery.validator.addMethod("bic", function(value, element) {
return this.optional( element ) || /^([A-Z]{6}[A-Z2-9][A-NP-Z1-2])(X{3}|[A-WY-Z0-9][A-Z0-9]{2})?$/.test( value );
}, "Please specify a valid BIC code");

122
src/additional/iban.js Normal file
View File

@@ -0,0 +1,122 @@
/**
* IBAN is the international bank account number.
* It has a country - specific format, that is checked here too
*/
jQuery.validator.addMethod("iban", function(value, element) {
// some quick simple tests to prevent needless work
if (this.optional(element)) {
return true;
}
if (!(/^([a-zA-Z0-9]{4} ){2,8}[a-zA-Z0-9]{1,4}|[a-zA-Z0-9]{12,34}$/.test(value))) {
return false;
}
// check the country code and find the country specific format
var iban = value.replace(/ /g,'').toUpperCase(); // remove spaces and to upper case
var countrycode = iban.substring(0,2);
var bbancountrypatterns = {
'AL': "\\d{8}[\\dA-Z]{16}",
'AD': "\\d{8}[\\dA-Z]{12}",
'AT': "\\d{16}",
'AZ': "[\\dA-Z]{4}\\d{20}",
'BE': "\\d{12}",
'BH': "[A-Z]{4}[\\dA-Z]{14}",
'BA': "\\d{16}",
'BR': "\\d{23}[A-Z][\\dA-Z]",
'BG': "[A-Z]{4}\\d{6}[\\dA-Z]{8}",
'CR': "\\d{17}",
'HR': "\\d{17}",
'CY': "\\d{8}[\\dA-Z]{16}",
'CZ': "\\d{20}",
'DK': "\\d{14}",
'DO': "[A-Z]{4}\\d{20}",
'EE': "\\d{16}",
'FO': "\\d{14}",
'FI': "\\d{14}",
'FR': "\\d{10}[\\dA-Z]{11}\\d{2}",
'GE': "[\\dA-Z]{2}\\d{16}",
'DE': "\\d{18}",
'GI': "[A-Z]{4}[\\dA-Z]{15}",
'GR': "\\d{7}[\\dA-Z]{16}",
'GL': "\\d{14}",
'GT': "[\\dA-Z]{4}[\\dA-Z]{20}",
'HU': "\\d{24}",
'IS': "\\d{22}",
'IE': "[\\dA-Z]{4}\\d{14}",
'IL': "\\d{19}",
'IT': "[A-Z]\\d{10}[\\dA-Z]{12}",
'KZ': "\\d{3}[\\dA-Z]{13}",
'KW': "[A-Z]{4}[\\dA-Z]{22}",
'LV': "[A-Z]{4}[\\dA-Z]{13}",
'LB': "\\d{4}[\\dA-Z]{20}",
'LI': "\\d{5}[\\dA-Z]{12}",
'LT': "\\d{16}",
'LU': "\\d{3}[\\dA-Z]{13}",
'MK': "\\d{3}[\\dA-Z]{10}\\d{2}",
'MT': "[A-Z]{4}\\d{5}[\\dA-Z]{18}",
'MR': "\\d{23}",
'MU': "[A-Z]{4}\\d{19}[A-Z]{3}",
'MC': "\\d{10}[\\dA-Z]{11}\\d{2}",
'MD': "[\\dA-Z]{2}\\d{18}",
'ME': "\\d{18}",
'NL': "[A-Z]{4}\\d{10}",
'NO': "\\d{11}",
'PK': "[\\dA-Z]{4}\\d{16}",
'PS': "[\\dA-Z]{4}\\d{21}",
'PL': "\\d{24}",
'PT': "\\d{21}",
'RO': "[A-Z]{4}[\\dA-Z]{16}",
'SM': "[A-Z]\\d{10}[\\dA-Z]{12}",
'SA': "\\d{2}[\\dA-Z]{18}",
'RS': "\\d{18}",
'SK': "\\d{20}",
'SI': "\\d{15}",
'ES': "\\d{20}",
'SE': "\\d{20}",
'CH': "\\d{5}[\\dA-Z]{12}",
'TN': "\\d{20}",
'TR': "\\d{5}[\\dA-Z]{17}",
'AE': "\\d{3}\\d{16}",
'GB': "[A-Z]{4}\\d{14}",
'VG': "[\\dA-Z]{4}\\d{16}"
};
var bbanpattern = bbancountrypatterns[countrycode];
// As new countries will start using IBAN in the
// future, we only check if the countrycode is known.
// This prevents false negatives, while almost all
// false positives introduced by this, will be caught
// by the checksum validation below anyway.
// Strict checking should return FALSE for unknown
// countries.
if (typeof bbanpattern !== 'undefined') {
var ibanregexp = new RegExp("^[A-Z]{2}\\d{2}" + bbanpattern + "$", "");
if (!(ibanregexp.test(iban))) {
return false; // invalid country specific format
}
}
// now check the checksum, first convert to digits
var ibancheck = iban.substring(4,iban.length) + iban.substring(0,4);
var ibancheckdigits = "";
var leadingZeroes = true;
var charAt;
for (var i =0; i<ibancheck.length; i++) {
charAt = ibancheck.charAt(i);
if (charAt !== "0") {
leadingZeroes = false;
}
if (!leadingZeroes) {
ibancheckdigits += "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".indexOf(charAt);
}
}
// calculate the result of: ibancheckdigits % 97
var cRest = '';
var cOperator = '';
for (var p=0; p<ibancheckdigits.length; p++) {
var cChar = ibancheckdigits.charAt(p);
cOperator = '' + cRest + '' + cChar;
cRest = cOperator % 97;
}
return cRest === 1;
}, "Please specify a valid IBAN");