Good afternoon, I need to write a method that will validate the phone number .
I need the number can only contain numbers (0-9) and these characters : "()" "-" "+"
, but everything else is forbidden!
For example :
+210-998-234-01234 -OK
210-998-234-01234-OK
+21099823401234-OK
+210-998-234-01234 -OK
+210-(998)-(234)-(01234) - OK
+210-99A-234-01234 - FALSE , +210-999-234-0""234 -FALSE , +210-999-234-02;4 - FALSE
If the number contains something unresolved it should throw exception. I wrote this method but for some reason it doesn't work right. Please explain where I made a mistake when using matches()
I will be grateful for your help!
public void validatePhoneNumber(String phoneNumber) {
if (!phoneNumber.matches("[ˆ0-9()\\-+]")) {
throw new IncorrectlyEnteredDataException("Phone number have unresolved characters");
}