Added BIC and IBAN validation

This commit is contained in:
SimonTuberlin 2017-01-30 22:43:15 +01:00
parent d569ca86d9
commit 8ddf670b8e
3 changed files with 97 additions and 4 deletions

2
.gitignore vendored
View File

@ -26,3 +26,5 @@ build
desktop.ini
*.bat
!package/windows/*bit*.bat
*/target/*
*.class

View File

@ -17,6 +17,24 @@
package io.bitsquare.gui.util.validation;
import java.util.Locale;
/*
* BIC information taken from German wikipedia (2017-01-30)
*
* length 8 or 11 characters
* General format: BBBB CC LL (bbb)
* with B - Bank code
* C - Country code
* L - Location code
* b - branch code (if applicable)
*
* B and C must be letters
* first L cannot be 0 or 1, second L cannot be O (upper case 'o')
* bbb cannot begin with X, unless it is XXX
*/
// TODO Special letters like ä, å, ... are not detected as invalid
public final class BICValidator extends InputValidator {
@ -29,8 +47,34 @@ public final class BICValidator extends InputValidator {
// TODO Add validation for primary and secondary IDs according to the selected type
// IBAN max 34 chars
// bic: max 11 char
return super.validate(input);
// bic: 8 or 11 chars
// check ensure length 8 or 11
if (!isStringWithFixedLength(input,8) && !isStringWithFixedLength(input,11))
return new ValidationResult(false, "Input length is neither 8 nor 11");
input = input.toUpperCase(Locale.ROOT);
// ensure Bank and Contry code to be letters only
for (int k=0; k<6; k++) {
if (!Character.isLetter(input.charAt(k)))
return new ValidationResult(false, "Bank and Country code must be letters");
}
// ensure location code starts not with 0 or 1 and ends not with O
char ch = input.charAt(6);
if (ch == '0' || ch == '1' || input.charAt(7) == 'O')
return new ValidationResult(false, "BIC contains invalid location code");
// check complete for 8 char BIC
if (input.length() == 8)
return new ValidationResult(true);
// ensure branch code does not start with X unless it is XXX
if (input.charAt(8) == 'X')
if (input.charAt(9) != 'X' || input.charAt(10) != 'X')
return new ValidationResult(false, "BIC contains invalid branch code");
return new ValidationResult(true);
}

View File

@ -17,6 +17,12 @@
package io.bitsquare.gui.util.validation;
import java.math.BigInteger;
import java.util.Locale;
import java.util.Arrays;
// TODO Does not yet recognize special letters like ä, ö, ü, å, ... as invalid characters
public final class IBANValidator extends InputValidator {
@ -28,9 +34,50 @@ public final class IBANValidator extends InputValidator {
public ValidationResult validate(String input) {
// TODO Add validation for primary and secondary IDs according to the selected type
// IBAN max 34 chars
// IBAN max 34 chars, shortest is Norwegian with 15 chars, BBAN may include letters
// bic: max 11 char
return super.validate(input);
// check input length first
if (isStringInRange(input, 15, 34)) {
input = input.toUpperCase(Locale.ROOT); // ensure upper case
// reorder IBAN to format <account number> <country code> <checksum>
String input2 = new String(input.substring(4, input.length()) + input.substring(0,4));
int charCount = 0;
char ch;
// check if input is alphanumeric and count included letters
for (int k=0; k<input2.length(); k++) {
ch = input2.charAt(k);
if (Character.isLetter(ch))
charCount++;
else if (!Character.isDigit(ch))
return (new ValidationResult(false, "Non-alphanumeric character detected"));
}
// create final char array for checksum validation
char [] charArray = new char[input2.length()+charCount];
int i = 0;
int tmp;
for (int k=0; k<input2.length(); k++) {
ch = input2.charAt(k);
if (Character.isLetter(ch)) {
tmp = ch - ('A' - 10); // letters are transformed to two digit numbers A->10, B->11, ...
String s = Integer.toString(tmp);
charArray[i++] = s.charAt(0); // insert transformed
charArray[i++] = s.charAt(1); // letters into char array
} else charArray[i++] = ch; // transfer digits directly to char array
}
// System.out.print(Arrays.toString(charArray) + '\t');
BigInteger bigInt = new BigInteger(new String(charArray));
int result = bigInt.mod(new BigInteger(Integer.toString(97))).intValue();
if (result == 1)
return new ValidationResult(true);
else
return new ValidationResult(false, "IBAN checksum is invalid");
}
// return new ValidationResult(false, BSResources.get("validation.accountNrChars", "15 - 34"));
return new ValidationResult(false, "Number must have length 15 to 34 chars.");
}