SHARE
TWEET

cnp validation

a guest Dec 9th, 2019 98 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. case REGISTER_CNP:
  2.     exactLengthError = getExactLengthErrorMessage(context, input.trim(), 13, inputTextType);
  3.     if (exactLengthError != null) return exactLengthError;
  4.  
  5.     String cnpRegex = "^d{1}d{2}(0[1-9]|1[0-2])(0[1-9]|[12]d|3[01])(0[1-9]|[1-4]d|5[0-2]|99)d{4}$";
  6.  
  7.     Pattern cnpPattern = Pattern.compile(cnpRegex);
  8.     Matcher cnpMatcher = cnpPattern.matcher(input.trim());
  9.  
  10.     String trimmedInput = input.trim();
  11.  
  12.     if (!cnpMatcher.matches()) {
  13.  
  14.         int bigSum = 0, ctrlDigit;
  15.         int control[] = {2, 7, 9, 1, 4, 6, 3, 5, 8, 2, 7, 9};
  16.  
  17.         for (int i = 0; i < control.length; i++) {
  18.             bigSum += Integer.parseInt(String.valueOf(trimmedInput.charAt(i))) * control[i];
  19.         }
  20.  
  21.         ctrlDigit = bigSum % 11;
  22.  
  23.         if (ctrlDigit == 10) {
  24.             ctrlDigit = 1;
  25.         }
  26.  
  27.         if (ctrlDigit != Integer.parseInt(String.valueOf(trimmedInput.charAt(12)))) {
  28.             return context.getString(R.string.input_error_invalid, hint);
  29.         }
  30.     }
  31.  
  32.     break;
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top