forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request eugenp#9148 from sergio41/master
[BAEL-3944] Validate Phone Numbers with Java Regex
- Loading branch information
Showing
2 changed files
with
129 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
...e-java-regex/src/test/java/com/baeldung/regex/phonenumbers/PhoneNumbersRegexUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package com.baeldung.regex.phonenumbers; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import org.junit.Test; | ||
|
||
public class PhoneNumbersRegexUnitTest { | ||
|
||
@Test | ||
public void whenMatchesTenDigitsNumber_thenCorrect() { | ||
Pattern pattern = Pattern.compile("^\\d{10}$"); | ||
Matcher matcher = pattern.matcher("2055550125"); | ||
assertTrue(matcher.matches()); | ||
} | ||
|
||
@Test | ||
public void whenMOreThanTenDigits_thenNotCorrect() { | ||
Pattern pattern = Pattern.compile("^\\d{10}$"); | ||
Matcher matcher = pattern.matcher("20555501251"); | ||
assertFalse(matcher.matches()); | ||
} | ||
|
||
@Test | ||
public void whenMatchesTenDigitsNumberWhitespacesDotHyphen_thenCorrect() { | ||
Pattern pattern = Pattern.compile("^(\\d{3}[- .]?){2}\\d{4}$"); | ||
Matcher matcher = pattern.matcher("202 555 0125"); | ||
assertTrue(matcher.matches()); | ||
} | ||
|
||
@Test | ||
public void whenIncludesBracket_thenNotCorrect() { | ||
Pattern pattern = Pattern.compile("^(\\d{3}[- .]?){2}\\d{4}$"); | ||
Matcher matcher = pattern.matcher("202]555 0125"); | ||
assertFalse(matcher.matches()); | ||
} | ||
|
||
@Test | ||
public void whenNotStartsWithBatchesOfThreeDigits_thenNotCorrect() { | ||
Pattern pattern = Pattern.compile("^(\\d{3}[- .]?){2}\\d{4}$"); | ||
Matcher matcher = pattern.matcher("2021 555 0125"); | ||
assertFalse(matcher.matches()); | ||
} | ||
|
||
@Test | ||
public void whenLastPartWithNoFourDigits_thenNotCorrect() { | ||
Pattern pattern = Pattern.compile("^(\\d{3}[- .]?){2}\\d{4}$"); | ||
Matcher matcher = pattern.matcher("202 555 012"); | ||
assertFalse(matcher.matches()); | ||
} | ||
|
||
@Test | ||
public void whenMatchesTenDigitsNumberParenthesis_thenCorrect() { | ||
Pattern pattern = Pattern.compile("^((\\(\\d{3}\\))|\\d{3})[- .]?\\d{3}[- .]?\\d{4}$"); | ||
Matcher matcher = pattern.matcher("(202) 555-0125"); | ||
assertTrue(matcher.matches()); | ||
} | ||
|
||
@Test | ||
public void whenJustOpeningParenthesis_thenNotCorrect() { | ||
Pattern pattern = Pattern.compile("^((\\(\\d{3}\\))|\\d{3})[- .]?\\d{3}[- .]?\\d{4}$"); | ||
Matcher matcher = pattern.matcher("(202 555-0125"); | ||
assertFalse(matcher.matches()); | ||
} | ||
|
||
@Test | ||
public void whenJustClosingParenthesis_thenNotCorrect() { | ||
Pattern pattern = Pattern.compile("^((\\(\\d{3}\\))|\\d{3})[- .]?\\d{3}[- .]?\\d{4}$"); | ||
Matcher matcher = pattern.matcher("202) 555-0125"); | ||
assertFalse(matcher.matches()); | ||
} | ||
|
||
@Test | ||
public void whenMatchesTenDigitsNumberPrefix_thenCorrect() { | ||
Pattern pattern = Pattern.compile("^(\\+\\d{1,3}( )?)?((\\(\\d{3}\\))|\\d{3})[- .]?\\d{3}[- .]?\\d{4}$"); | ||
Matcher matcher = pattern.matcher("+111 (202) 555-0125"); | ||
assertTrue(matcher.matches()); | ||
} | ||
|
||
@Test | ||
public void whenIncorrectPrefix_thenNotCorrect() { | ||
Pattern pattern = Pattern.compile("^(\\+\\d{1,3}( )?)?((\\(\\d{3}\\))|\\d{3})[- .]?\\d{3}[- .]?\\d{4}$"); | ||
Matcher matcher = pattern.matcher("-111 (202) 555-0125"); | ||
assertFalse(matcher.matches()); | ||
} | ||
|
||
@Test | ||
public void whenTooLongPrefix_thenNotCorrect() { | ||
Pattern pattern = Pattern.compile("^(\\+\\d{1,3}( )?)?((\\(\\d{3}\\))|\\d{3})[- .]?\\d{3}[- .]?\\d{4}$"); | ||
Matcher matcher = pattern.matcher("+1111 (202) 555-0125"); | ||
assertFalse(matcher.matches()); | ||
} | ||
|
||
@Test | ||
public void whenMatchesPhoneNumber_thenCorrect() { | ||
String patterns | ||
= "^(\\+\\d{1,3}( )?)?((\\(\\d{3}\\))|\\d{3})[- .]?\\d{3}[- .]?\\d{4}$" | ||
+ "|^(\\+\\d{1,3}( )?)?(\\d{3}[ ]?){2}\\d{3}$" | ||
+ "|^(\\+\\d{1,3}( )?)?(\\d{3}[ ]?)(\\d{2}[ ]?){2}\\d{2}$"; | ||
|
||
String[] validPhoneNumbers | ||
= {"2055550125","202 555 0125", "(202) 555-0125", "+111 (202) 555-0125", "636 856 789", "+111 636 856 789", "636 85 67 89", "+111 636 85 67 89"}; | ||
|
||
Pattern pattern = Pattern.compile(patterns); | ||
for(String phoneNumber : validPhoneNumbers) { | ||
Matcher matcher = pattern.matcher(phoneNumber); | ||
assertTrue(matcher.matches()); | ||
} | ||
} | ||
|
||
@Test | ||
public void whenNotMatchesPhoneNumber_thenNotCorrect() { | ||
String patterns | ||
= "^(\\+\\d{1,3}( )?)?((\\(\\d{3}\\))|\\d{3})[- .]?\\d{3}[- .]?\\d{4}$" | ||
+ "|^(\\+\\d{1,3}( )?)?(\\d{3}[ ]?){2}\\d{3}$" | ||
+ "|^(\\+\\d{1,3}( )?)?(\\d{3}[ ]?)(\\d{2}[ ]?){2}\\d{2}$"; | ||
|
||
String[] invalidPhoneNumbers | ||
= {"20555501251","202]555 0125", "2021 555 012", "(202 555-0125", "202) 555-0125", "-111 (202) 555-0125", "+1111 (202) 555-0125", "636 85 789", "636 85 67 893"}; | ||
|
||
Pattern pattern = Pattern.compile(patterns); | ||
for(String phoneNumber : invalidPhoneNumbers) { | ||
Matcher matcher = pattern.matcher(phoneNumber); | ||
assertFalse(matcher.matches()); | ||
} | ||
} | ||
} |