Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

33
LINES

< > BotCompany Repo | #1029747 // InternationalPhoneValidator

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (5412L/36K).

// see https://howtodoinjava.com/java/regex/java-regex-validate-international-phone-numbers/
sclass InternationalPhoneValidator {
  S rawInput;
  S filtered;
  L<CountryDialCode> dialCodes; // might be multiple countries with same code
  S countryPart, localPart;
  bool valid;
  S error;
  
  *() {}
  *(S *rawInput) {}
  
  // needs rawInput
  run {
    filtered = dropPrefix("+", dropSpecialChars(rawInput));
    if (!isAllDigits(filtered)) ret with error = "Bad characters";
    splitLocal();
  }
  
  S dropSpecialChars(S s) {
    ret replaceAll(s, "[\\s\\(\\)\\-]", "");
  }
  
  void splitLocal {
    S countryPart = longestPrefixInNavigableSet(filtered, navigableKeys(countryDialCodes_rawNumbersTreeMultiMap()));
    if (countryPart == null) ret with error = "Invalid country code";
    dialCodes = countryDialCodes_rawNumbersTreeMultiMap().get(countryPart);
    localPart = dropPrefix(countryPart, filtered);
    if (l(filtered) < 7) ret with error = "Phone number too short";
    else if (l(filtered) > 15) ret with error = "Phone number too long";
    valid = true;
  }
}

Author comment

Began life as a copy of #1023353

download  show line numbers  debug dex  old transpilations   

Travelled to 6 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1029747
Snippet name: InternationalPhoneValidator
Eternal ID of this version: #1029747/9
Text MD5: bf896d0f85600c76524c3d4e8002a46f
Transpilation MD5: c6df33009f3e801b8072e555c4aeeddc
Author: stefan
Category: javax / mail
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-09-11 15:20:06
Source code size: 1114 bytes / 33 lines
Pitched / IR pitched: No / No
Views / Downloads: 210 / 476
Version history: 8 change(s)
Referenced in: #1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674)