Last active
July 9, 2018 14:29
-
-
Save steklopod/d84f18365dc5c1dffc438f2c22801bba to your computer and use it in GitHub Desktop.
7 параметров
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.task.task22.task2212; | |
/* | |
Проверка номера телефона | |
Критерии валидности: | |
1) если номер начинается с ‘+‘, то он содержит 12 цифр | |
2) если номер начинается с цифры или открывающей скобки, то он содержит 10 цифр | |
3) может содержать 0-2 знаков ‘—‘, которые не могут идти подряд | |
4) может содержать 1 пару скобок ‘(‘ и ‘)‘ , причем если она есть, то она расположена левее знаков ‘-‘ | |
5) скобки внутри содержат четко 3 цифры | |
6) номер не содержит букв | |
7) номер заканчивается на цифру | |
*/ | |
public class Solution { | |
public static boolean checkTelNumber(String telNumber) { | |
if (telNumber == null) return false; | |
if (telNumber.isEmpty()) return false; | |
int digits = telNumber.replaceAll("\\D", "").length(); | |
if ((telNumber.charAt(0) == '+' && digits == 12) || (telNumber.charAt(0) != '+' && digits == 10)) { | |
return telNumber.matches("(\\+\\d+)?\\d*(\\(\\d{3}\\))?\\d+(-?\\d+){0,2}"); | |
} | |
else return false; | |
} | |
public static void main(String[] args) { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment