w3resource

Java: Validate a given phone number


Write a Java program to validate a phone number.

Following are valid phone number examples:
“(123)4567890", "1234567890", "123-456-7890", "(123)456-7890",
Following are invalid phone numbers:
"(1234567890)","123)4567890", "12345678901", "(1)234567890", "(123)-4567890", "1", "12-3456-7890", "123-4567", "Hello world"};

    Regex explanation:
  • ^\\(? - May start with an option "("
  • (\\d{3}) - Followed by 3 digits
  • \\)? - May have an optional ")"
  • [- ]? - May have an optional "-" after the first 3 digits or after optional ) character
  • (\\d{3}) - Followed by 3 digits.
  • [- ]? - May have another optional "-" after numeric digits
  • (\\d{4})$ - ends with four digits

Sample Solution:

Java Code:

//Ref.https://bit.ly/33gB1TY
public class test {
 
   public static void main(String[] args) {	   
	    String text ="(123)4567890";
		System.out.println("Original Phone number: "+text);
		System.out.println("Check the said Phone number is true or not! "+validate(text));
	    text ="(123)4567890";
		System.out.println("\nOriginal Phone number: "+text);
		System.out.println("Check the said Phone number is true or not! "+validate(text));
	    text ="1234567890";
		System.out.println("\nOriginal Phone number: "+text);
		System.out.println("Check the said Phone number is true or not! "+validate(text));		
	    text ="123-456-7890";
		System.out.println("\nOriginal Phone number: "+text);
		System.out.println("Check the said Phone number is true or not! "+validate(text));		
	    text ="(1234567890)";
		System.out.println("\nOriginal Phone number: "+text);
		System.out.println("Check the said Phone number is true or not! "+validate(text));		
	    text ="123)4567890";
		System.out.println("\nOriginal Phone number: "+text);
		System.out.println("Check the said Phone number is true or not! "+validate(text));		
	    text ="12345678901";
		System.out.println("\nOriginal Phone number: "+text);
		System.out.println("Check the said Phone number is true or not! "+validate(text));		
	    text ="(1)234567890";
		System.out.println("\nOriginal Phone number: "+text);
		System.out.println("Check the said Phone number is true or not! "+validate(text));		
	    text ="(123)-4567890";
		System.out.println("\nOriginal Phone number: "+text);
		System.out.println("Check the said Phone number is true or not! "+validate(text));		
	    text ="1";
		System.out.println("\nOriginal Phone number: "+text);
		System.out.println("Check the said Phone number is true or not! "+validate(text));		
	    text ="12-3456-7890";
		System.out.println("\nOriginal Phone number: "+text);
		System.out.println("Check the said Phone number is true or not! "+validate(text));
	    text ="123-4567";
		System.out.println("\nOriginal Phone number: "+text);
		System.out.println("Check the said Phone number is true or not! "+validate(text));
	}
   public static Boolean validate(String text) {
        return text.matches("\\d{10}|(?:\\d{3}-){2}\\d{4}|\\(\\d{3}\\)\\d{3}-?\\d{4}");
   }
}

Sample Output:

Original Phone number: (123)4567890
Check the said Phone number is true or not! true

Original Phone number: (123)4567890
Check the said Phone number is true or not! true

Original Phone number: 1234567890
Check the said Phone number is true or not! true

Original Phone number: 123-456-7890
Check the said Phone number is true or not! true

Original Phone number: (1234567890)
Check the said Phone number is true or not! false

Original Phone number: 123)4567890
Check the said Phone number is true or not! false

Original Phone number: 12345678901
Check the said Phone number is true or not! false

Original Phone number: (1)234567890
Check the said Phone number is true or not! false

Original Phone number: (123)-4567890
Check the said Phone number is true or not! false

Original Phone number: 1
Check the said Phone number is true or not! false

Original Phone number: 12-3456-7890
Check the said Phone number is true or not! false

Original Phone number: 123-4567
Check the said Phone number is true or not! false

Flowchart :

Flowchart: Validate a given phone number.


For more Practice: Solve these Related Problems:

  • Write a Java program to validate a phone number format that allows optional country codes and separators using regex.
  • Write a Java program to check if a string is a valid phone number based on a specified pattern with dashes and parentheses.
  • Write a Java program to implement a method that returns true if the input string matches a standardized phone number format.
  • Write a Java program to validate phone numbers that may include spaces, dashes, and brackets using a comprehensive regex.

Java Code Editor:

Contribute your code and comments through Disqus.

Previous: Remove all non-alphanumeric characters from a given string.

Next: Move all lower case letters to the front, keeping the order of all the letters of a given word.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.