- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to check valid mobile number using Java regular expressions
You can match a valid mobile number using the following regular expression −
"\d{10}"
- A valid mobile number generally have 10 digits (in India).
- The metacharacter "\d" matches the digits from 0 to 9.
- The quantifier ex{n} suggests the repetition of ex n times.
Example 1
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class PhoneNumberExample { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter your name: "); String name = sc.nextLine(); System.out.println("Enter your Phone number: "); String phone = sc.next(); //Regular expression to accept valid phone number String regex = "\d{10}"; //Creating a pattern object Pattern pattern = Pattern.compile(regex); //Creating a Matcher object Matcher matcher = pattern.matcher(phone); //Verifying whether given phone number is valid if(matcher.matches()) { System.out.println("Given phone number is valid"); } else { System.out.println("Given phone number is not valid"); } } }
Output 1
Enter your name: krishna Enter your Phone number: 9848022338 Given phone number is valid
Output 2
Enter your name: krishna Enter your Phone number: 5465 Given phone number is not valid
Output 3
Enter your name: krishna Enter your Phone number: 984802354655 Given phone number is not valid
Example 2
import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter your Phone number: "); String phone = sc.next(); //Regular expression to accept valid phone number String regex = "\d{10}"; //Matching the given phone number with regular expression boolean result = phone.matches(regex); if(result) { System.out.println("Given phone number is valid"); } else { System.out.println("Given phone number is not valid"); } } }
Output 1
Enter your Phone number: 9848022338 Given phone number is valid
Output 2
Enter your Phone number: 123 Given phone number is not valid
Output 3
Enter your Phone number: 123654788755 Given phone number is not valid
- Related Articles
- Checking for valid email address using regular expressions in Java
- Phone Number validation using Java Regular Expressions
- Java Program to check if a string is a valid number
- Date validation using Java Regular Expressions
- Name validation using Java Regular Expressions
- Zip Code validation using Java Regular Expressions
- How to validate an email address using Java regular expressions.
- Java Regular Expressions Tutorial
- How to use regular expressions (Regex) to filter valid emails in a Pandas series?
- Python program to check credit card number is valid or not
- Java regular expressions sample examples
- Possessive quantifiers Java Regular expressions
- Java Regular expressions Logical operators
- Reluctant quantifiers Java Regular expressions
- Java regular expression program to validate an email including blank field valid as well

Advertisements