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).

1  
// see https://howtodoinjava.com/java/regex/java-regex-validate-international-phone-numbers/
2  
sclass InternationalPhoneValidator {
3  
  S rawInput;
4  
  S filtered;
5  
  L<CountryDialCode> dialCodes; // might be multiple countries with same code
6  
  S countryPart, localPart;
7  
  bool valid;
8  
  S error;
9  
  
10  
  *() {}
11  
  *(S *rawInput) {}
12  
  
13  
  // needs rawInput
14  
  run {
15  
    filtered = dropPrefix("+", dropSpecialChars(rawInput));
16  
    if (!isAllDigits(filtered)) ret with error = "Bad characters";
17  
    splitLocal();
18  
  }
19  
  
20  
  S dropSpecialChars(S s) {
21  
    ret replaceAll(s, "[\\s\\(\\)\\-]", "");
22  
  }
23  
  
24  
  void splitLocal {
25  
    S countryPart = longestPrefixInNavigableSet(filtered, navigableKeys(countryDialCodes_rawNumbersTreeMultiMap()));
26  
    if (countryPart == null) ret with error = "Invalid country code";
27  
    dialCodes = countryDialCodes_rawNumbersTreeMultiMap().get(countryPart);
28  
    localPart = dropPrefix(countryPart, filtered);
29  
    if (l(filtered) < 7) ret with error = "Phone number too short";
30  
    else if (l(filtered) > 15) ret with error = "Phone number too long";
31  
    valid = true;
32  
  }
33  
}

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: 149 / 391
Version history: 8 change(s)
Referenced in: [show references]