Hi Vinay, ·   · Please find the snippet below, wrote the code in view of US mobile phone number validation · var phoneNum = "+14445551234" · phoneNum = phoneNum.replace(/ /g, ""); //Remove all spaces from phone number · var validUSPhoneExp = /^((\+1 ?)?((\(\d{3}\))|(\d{3}) ?\-) ?(\d{3}) *\- *\d{4})|((\+1 ?)?)\d{10}$/; · var phoneNumWithoutDelimeters = phoneNum.replace(/( |\(|\)|\-)/g,""); · var invalidPattern1 = /^(\+1)?(1{10}|2{10}|3{10}|4{10}|5{10}|6{10}|7{10}|8{10}|9{10}|0{10})$/; //Phone number with same digits · var invalidPattern2 = /^(\+1)?(\d{3}(555)\d{4})$/; // US mobile numbers used in movies or TV have 555 in the 4th 5th and 6th digits of the phone numbers. Treating these as invalid. · var result = validUSPhoneExp.test(phoneNum) && !invalidPattern1.test(phoneNumWithoutDelimeters) && !invalidPattern2.test(phoneNumWithoutDelimeters); · var finalNum = ""; · if(result == true) { · // phoneNum varible has correct phone number available. · } · Above code validates for below formats returns true in the result with some exceptions noted below these formats.(xxx)xxx-xxx+1(xxx)xxx-xxx(xxx) xxx-xxx(xxx) xxx -xxxxxx-xxx-xxxxxx - xxx - xxxxxx -xxx -xxx+1xxx -xxx -xxx+1xxxxxxxxxx · But if the mobile numbers have 555 in the 6,7,8 digits, they are used in movies in US and they are not allocated to anyone. Above code treats them as invalid phone numbers.If the US mobile phone or 10 digit mobile number has same number in all the places they are treated as invalid numbers.For example, all the numbers below are treated as invalid.+11111111111+1(111)111-1111+1111-111-1111+1 111-111-1111+11115551234+1111-555-1234 · Thanks and regards, · Subrahmanyam Satti Answer from Subrahmanyam2 on servicenow.com
Top answer
1 of 2
1
Hi Vinay, ·   · Please find the snippet below, wrote the code in view of US mobile phone number validation · var phoneNum = "+14445551234" · phoneNum = phoneNum.replace(/ /g, ""); //Remove all spaces from phone number · var validUSPhoneExp = /^((\+1 ?)?((\(\d{3}\))|(\d{3}) ?\-) ?(\d{3}) *\- *\d{4})|((\+1 ?)?)\d{10}$/; · var phoneNumWithoutDelimeters = phoneNum.replace(/( |\(|\)|\-)/g,""); · var invalidPattern1 = /^(\+1)?(1{10}|2{10}|3{10}|4{10}|5{10}|6{10}|7{10}|8{10}|9{10}|0{10})$/; //Phone number with same digits · var invalidPattern2 = /^(\+1)?(\d{3}(555)\d{4})$/; // US mobile numbers used in movies or TV have 555 in the 4th 5th and 6th digits of the phone numbers. Treating these as invalid. · var result = validUSPhoneExp.test(phoneNum) && !invalidPattern1.test(phoneNumWithoutDelimeters) && !invalidPattern2.test(phoneNumWithoutDelimeters); · var finalNum = ""; · if(result == true) { · // phoneNum varible has correct phone number available. · } · Above code validates for below formats returns true in the result with some exceptions noted below these formats.(xxx)xxx-xxx+1(xxx)xxx-xxx(xxx) xxx-xxx(xxx) xxx -xxxxxx-xxx-xxxxxx - xxx - xxxxxx -xxx -xxx+1xxx -xxx -xxx+1xxxxxxxxxx · But if the mobile numbers have 555 in the 6,7,8 digits, they are used in movies in US and they are not allocated to anyone. Above code treats them as invalid phone numbers.If the US mobile phone or 10 digit mobile number has same number in all the places they are treated as invalid numbers.For example, all the numbers below are treated as invalid.+11111111111+1(111)111-1111+1111-111-1111+1 111-111-1111+11115551234+1111-555-1234 · Thanks and regards, · Subrahmanyam Satti
2 of 2
0
var number = g_form.getValue('field_name'); · var check = /^[0-9]{10}/; · var result = number.match(check); · if(result==true) · { g_form.setValue('field_name', 'number'); } · else · { alert("Dear Requestor, please make sure that the number you provide is 10 digits long"); } ·   · please mark helpful /correct based on impact · Regards, · Aniket S
🌐
CodingNConcepts
codingnconcepts.com › java › java-regex-to-validate-phone-number
Java Regex to Validate Phone Number - Coding N Concepts
May 27, 2020 - String regex = "^\\d{10}$"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher("9876543210"); matcher.matches(); // returns true if pattern matches, else returns false ...
🌐
TutorialsPoint
tutorialspoint.com › program-to-check-valid-mobile-number-using-java-regular-expressions
Program to check valid mobile number using Java regular expressions
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 ...
🌐
regex101
regex101.com › library › cG4kD9
regex101: 10-digit number
Mobile Networks: (Vodafone: 10, e&:11, Orange:12, we:15) Operator Code: The phone number must be followed by one of the valid operator prefixes (10, 11, 12, or 15), corresponding to major mobile networks in Egypt. Number Length: The phone number must have exactly 8 digits following the operator code, for a total of 11 digits (including the country code and operator code).
Top answer
1 of 5
24

Basically, you need to take 3 or 4 different patterns and combine them with "|":

String pattern = "\\d{10}|(?:\\d{3}-){2}\\d{4}|\\(\\d{3}\\)\\d{3}-?\\d{4}";
  • \d{10} matches 1234567890
  • (?:\d{3}-){2}\d{4} matches 123-456-7890
  • \(\d{3}\)\d{3}-?\d{4} matches (123)456-7890 or (123)4567890
2 of 5
8

Considering these facts about phone number format:-

  1. Country Code prefix starts with ‘+’ and has 1 to 3 digits
  2. Last part of the number, also known as subscriber number is 4 digits in all of the numbers
  3. Most of the countries have 10 digits phone number after excluding country code. A general observation is that all countries phone number falls somewhere between 8 to 11 digits after excluding country code.
String allCountryRegex = "^(\\+\\d{1,3}( )?)?((\\(\\d{1,3}\\))|\\d{1,3})[- .]?\\d{3,4}[- .]?\\d{4}$";

Let's break the regex and understand,

  • ^ start of expression
  • (\\+\\d{1,3}( )?)? is optional match of country code between 1 to 3 digits prefixed with '+' symbol, followed by space or no space.
  • ((\\(\\d{1,3}\\))|\\d{1,3} is mandatory group of 1 to 3 digits with or without parenthesis followed by hyphen, space or no space.
  • \\d{3,4}[- .]? is mandatory group of 3 or 4 digits followed by hyphen, space or no space
  • \\d{4} is mandatory group of last 4 digits
  • $ end of expression

This regex pattern matches most of the countries phone number format including these:-

        String Afghanistan      = "+93 30 539-0605";
        String Australia        = "+61 2 1255-3456";
        String China            = "+86 (20) 1255-3456";
        String Germany          = "+49 351 125-3456";
        String India            = "+91 9876543210";
        String Indonesia        = "+62 21 6539-0605";
        String Iran             = "+98 (515) 539-0605";
        String Italy            = "+39 06 5398-0605";
        String NewZealand       = "+64 3 539-0605";
        String Philippines      = "+63 35 539-0605";
        String Singapore        = "+65 6396 0605";
        String Thailand         = "+66 2 123 4567";
        String UK               = "+44 141 222-3344";
        String USA              = "+1 (212) 555-3456";
        String Vietnam          = "+84 35 539-0605";

Source:https://codingnconcepts.com/java/java-regex-for-phone-number/

🌐
Baeldung
baeldung.com › home › java › validate phone numbers with java regex
Validate Phone Numbers With Java Regex | Baeldung
January 8, 2024 - Let’s start with a simple expression ... else: @Test public void whenMatchesTenDigitsNumber_thenCorrect() { Pattern pattern = Pattern.compile("^\\d{10}$"); Matcher matcher = pattern.matcher("2055550125"); assertTrue(matcher.matches()); }...
🌐
Raffles Educity
raffleseducity.com › wp-content › cache › 9on0q › regular-expression-for-10-digit-mobile-number-in-java-242c46
regular expression for 10 digit mobile number in java
January 20, 2021 - RegExp Object. values.add("123... with a fixed number of digits. The [0-9] expression is used to find any character between the brackets. Plese Help in … This video discusses how to validate a Mobile number using Regular Expression (Regex) in Java Netbeans....
🌐
regex101
regex101.com › library › 1D4Mpk
regex101: 10 digit phone number
Matches Name Desc Y (+ - . :space:) LEADING_PUNCT Leading Punctuation Y 1-3 Digits (0-9) COUNTRY_CODE International country code (US: '1') Y (- . :space:) OPT_DELIM_1 Delimiting Punctuation Y 1-3 Digits (0-9) AREA_CODE Area Code/Regional Routing Code Y (- . :space:) OPT_DELIM_2 Delimiting Punctuation N 3 Digits (0-9) PREFIX Number Prefix (First 3 Numbers) Y (- .
Find elsewhere
🌐
RegExr
regexr.com › 3aa8n
10 digit phone no
RegExr is an online tool to learn, build, & test Regular Expressions (RegEx / RegExp).
🌐
Trestle
trestleiq.com › home › product › phone validation regex: the what, how, and pros and cons
Phone Validation Regex: The What, How, and Pros and Cons
September 24, 2025 - For example, a simple check, like if the phone is 10 or 11 digits and only contains specific or special characters, is an easy way to check that the number provided is valid. Without a high degree of confidence, time and resources are wasted on chasing down leads or reaching out to individuals with bad data. This can be done using what we call regex in a programming language. While regex (regular expression) is not the only tool you have to validate phone numbers, it gives you valuable insights into the potential legitimacy of the data you are ingesting at the onset without any costs involved and is pretty straightforward to implement.
🌐
CodeProject
codeproject.com › Questions › 1237801 › How-to-check-only-digit-mobile-number-regex
https://www.codeproject.com/Questions/1237801/How-...
Do not try and find the page. That’s impossible. Instead only try to realise the truth - For those who code; Updated: 1 Jul 2007
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-check-for-a-valid-mobile-number
Java Program to Check For a Valid Mobile Number - GeeksforGeeks
July 23, 2025 - // Java Program to Check For a ... the next 9 digits are any digits between 0 and 9. "$" denotes the end of the string, ensuring that the number is exactly 10 digits long....
Top answer
1 of 16
408
^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$

Matches the following

123-456-7890
(123) 456-7890
123 456 7890
123.456.7890
+91 (123) 456-7890

If you do not want a match on non-US numbers use

^(\+0?1\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$

Update :
As noticed by user Simon Weaver below, if you are also interested in matching on unformatted numbers just make the separator character class optional as [\s.-]?

^(\+\d{1,2}\s?)?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}$

https://regex101.com/r/j48BZs/2

2 of 16
247

There are many variations possible for this problem. Here is a regular expression similar to an answer I previously placed on SO.

^\s*(?:\+?(\d{1,3}))?[-. (]*(\d{3})[-. )]*(\d{3})[-. ]*(\d{4})(?: *x(\d+))?\s*$

It would match the following examples and much more:

18005551234
1 800 555 1234
+1 800 555-1234
+86 800 555 1234
1-800-555-1234
1 (800) 555-1234
(800)555-1234
(800) 555-1234
(800)5551234
800-555-1234
800.555.1234
800 555 1234x5678
8005551234 x5678
1    800    555-1234
1----800----555-1234

Regardless of the way the phone number is entered, the capture groups can be used to breakdown the phone number so you can process it in your code.

  • Group1: Country Code (ex: 1 or 86)
  • Group2: Area Code (ex: 800)
  • Group3: Exchange (ex: 555)
  • Group4: Subscriber Number (ex: 1234)
  • Group5: Extension (ex: 5678)

Here is a breakdown of the expression if you're interested:

^\s*                #Line start, match any whitespaces at the beginning if any.
(?:\+?(\d{1,3}))?   #GROUP 1: The country code. Optional.
[-. (]*             #Allow certain non numeric characters that may appear between the Country Code and the Area Code.
(\d{3})             #GROUP 2: The Area Code. Required.
[-. )]*             #Allow certain non numeric characters that may appear between the Area Code and the Exchange number.
(\d{3})             #GROUP 3: The Exchange number. Required.
[-. ]*              #Allow certain non numeric characters that may appear between the Exchange number and the Subscriber number.
(\d{4})             #Group 4: The Subscriber Number. Required.
(?: *x(\d+))?       #Group 5: The Extension number. Optional.
\s*$                #Match any ending whitespaces if any and the end of string.

To make the Area Code optional, just add a question mark after the (\d{3}) for the area code.

🌐
Qodex
qodex.ai › home › all tools › getting started › phone number regex java validator
Phone Number Regex Java Validator — Test Patterns Online
Because not every valid phone number shares the same structure. For instance, some might include country codes (+1 234 567 8901), while others stick with local 10-digit formats or use unique groupings and separators.
Rating: 4.9 ​ - ​ 60 votes
🌐
Mkyong
mkyong.com › home › java › how to validate phone number in java (regular expression)
How to validate phone number in Java (regular expression) - Mkyong.com
August 30, 2012 - All phone numbers must in “xxx-xxxxxxx” format. For example 1) 012-6677889 – Passed 2) 01216677889 – Failed , “-” missing 3) A12-6677889 – Failed , only digit allow 4) 012-66778899 – Failed, only 7 digits at the end · Full source code of phone number validation in Java
Top answer
1 of 4
4

The question is somewhat unclear, but I presume you want to split the number and the country code.

This is quite easy to do by extracting groups. group(i) is the i-th thing in brackets.

I also applied these simplifications: [\\d] = \\d, {0,1} = ?, [+] = \\+, [0]{2} = 00.

Code:

String regex = "^((\\+|00)(\\d{1,3})[\\s-]?)?(\\d{10})$";
String str = "+123-9854875847";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(str);
if (m.matches())
{
   System.out.println("Country = " + m.group(3));
   System.out.println("Data = " + m.group(4));
}

Output:

Country = 123
Data = 9854875847

Alternative using non-matching groups (?:): (so you can use group(1) and group(2))

String regex = "^(?:(?:\\+|00)(\\d{1,3})[\\s-]?)?(\\d{10})$";
String str = "+123-9854875847";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(str);
if (m.matches())
{
   System.out.println("Country = " + m.group(1));
   System.out.println("Data = " + m.group(2));
}

Reference.

Related test.

2 of 4
1

As long as the extension is always separated from the rest of the phone number, your regex will work fine. If there is no such separation, there is no way to correctly validate a phone number.

Also keep in mind that both extensions and phone numbers can vary in length from country to country, so there is no regex that will solve all cases. If you can produce a list of allowed extensions, you can work that into the regex and get better matches, but for many groups of arbitrary length of digits you will get many wrong matches.

I have simplified your regex a bit, so oyu can see @Dukeling's suggestions in practice. Your regex on top, mine on the bottom.

^(([+]|[0]{2})([\\d]{1,3})([\\s-]{0,1}))?([\\d]{10})$
^(  (\\+|00)    \\d{1,3}    [\\s-]?)?      \\d{10}  $
🌐
Stack Abuse
stackabuse.com › java-regular-expressions-validate-phone-number
Java Regular Expressions - Validate Phone Number
November 23, 2021 - In this short article, we'll take a look at how to validate a phone number in Java, using the regex package and multiple Regular Expressions.