Repository navigation
Expand file tree
/
Copy pathSolution.java
More file actions
54 lines (46 loc) · 1.8 KB
/
Copy pathSolution.java
File metadata and controls
54 lines (46 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package com.javarush.task.task22.task2212;
/*
Проверка номера телефона
*/
public class Solution {
public static boolean checkTelNumber(String telNumber) {
if (telNumber == null) {
return false;
}
if (!telNumber.matches("[0-9+\\-()]+\\d$")) {
return false;
}
if (telNumber.contains("(") || telNumber.contains(")")) {
if (!telNumber.matches(".*\\(\\d{3}\\).*")) {
return false;
}
}
if (telNumber.contains("-")) {
if (!telNumber.matches("[0-9+()]+(-\\d+){0,2}") ) {
return false;
}
}
if (telNumber.matches("^\\+.*")) {
if (telNumber.replaceAll("[^0-9]", "").length() != 12) {
return false;
}
}
if (telNumber.matches("^\\d.*")) {
if (telNumber.replaceAll("[^0-9]", "").length() != 10) {
return false;
}
}
return true;
}
public static void main(String[] args) {
System.out.println("+380501234567 - " + checkTelNumber("+380501234567"));
System.out.println("+38(050)1234567 - " + checkTelNumber("+38(050)1234567"));
System.out.println("+38050123-45-67 - " + checkTelNumber("+38050123-45-67"));
System.out.println("050123-4567 - " + checkTelNumber("050123-4567"));
System.out.println("+38)050(1234567 - " + checkTelNumber("+38)050(1234567"));
System.out.println("+38(050)1-23-45-6-7 - " + checkTelNumber("+38(050)1-23-45-6-7"));
System.out.println("050ххх4567 - " + checkTelNumber("050ххх4567"));
System.out.println("050123456 - " + checkTelNumber("050123456"));
System.out.println("(0)501234567 - " + checkTelNumber("(0)501234567"));
}
}