M
M
Monti12018-02-23 13:26:18
Java
Monti1, 2018-02-23 13:26:18

Phone number verification with Matcher, Pattern?

It is necessary so that until the user enters the correct number, he will not be able to exit the code block. Is there a way to use the "while" statement, or is there another way to implement it?

public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Введите свой моб. телефон. Пример: +380994563477");
        String phoneNumber = scan.nextLine();
        Pattern pattern = Pattern.compile("^((\\+?+380)([0-9]{9}))$");
        Matcher matcher = pattern.matcher(phoneNumber);
        if (matcher.matches()) {
            System.out.println("Phone: " + phoneNumber + " is correct!"); //введен корректныый номер
        }else {
            System.out.println("Phone: " + phoneNumber + " no correct!"); //введен некорректный номер
        }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey, 2018-02-23
@Monti1

If you need a correct number - only a verification code via SMS.
Or they will enter some +79005675421, and what?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question