Skip to content

Commit

Permalink
* [#1903] fix(phoneValidation): fix validation to allow one number in…
Browse files Browse the repository at this point in the history
… the optional sub-zone

* [#1903] test(phoneValidation): Add some test cases
  • Loading branch information
xael-fry committed Feb 4, 2015
1 parent b4e9c02 commit 0a03bf7
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
2 changes: 1 addition & 1 deletion framework/src/play/data/validation/PhoneCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class PhoneCheck extends AbstractAnnotationCheck<Phone> {

final static String mes = "validation.phone";

static Pattern phonePattern = Pattern.compile("^([\\+][0-9]{1,3}([ \\.\\-]))?([\\(]{1}[0-9]{2,6}[\\)])?([0-9 \\.\\-/]{3,20})((x|ext|extension)[ ]?[0-9]{1,4})?$");
static Pattern phonePattern = Pattern.compile("^([\\+][0-9]{1,3}([ \\.\\-]))?([\\(]{1}[0-9]{1,6}[\\)])?([0-9 \\.\\-/]{3,20})((x|ext|extension)[ ]?[0-9]{1,4})?$");

@Override
public void configure(Phone phone) {
Expand Down
60 changes: 60 additions & 0 deletions framework/test-src/play/data/validation/PhoneTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package play.data.validation;

import org.junit.Test;

import play.data.validation.Validation.ValidationResult;
import play.i18n.MessagesBuilder;
import static org.junit.Assert.assertEquals;

public class PhoneTest {

@Test
public void validPhoneValidationTest() {
new MessagesBuilder().build();
Validation.current.set(new Validation());

// US
assertValidPhone(true, "(305) 613 09 58 ext 101");
assertValidPhone(true, "(305) 613 09 58 extension 101");
// France
assertValidPhone(true, "+33 (0)147376224");
assertValidPhone(true, "+33 0147376224");
assertValidPhone(true, "+33 1 47 37 62 24 x3");
assertValidPhone(true, "+33 (0)1 47 37 62 24 x3");
assertValidPhone(true, "+33 0147376224 x3");
// Germany
assertValidPhone(true, "+49-4312 / 777 777");
assertValidPhone(true, "+49-4312 / 777 777");
// China
assertValidPhone(true, "+86 (10)69445464");
assertValidPhone(true, "+86 (00)69445464");

// UK
assertValidPhone(true, "(020) 1234 1234");
}

@Test
public void invalidalidPhoneValidationTest() {
new MessagesBuilder().build();
Validation.current.set(new Validation());

assertValidPhone(false, "(305) abc 09 58 ext 101");
// France
assertValidPhone(false, "+330147376224");
assertValidPhone(false, "+33(0)147376224");
assertValidPhone(false, "+33 (0)1 47 37 62 24 x3");

// China
assertValidPhone(false, "+86(10)69445464");
assertValidPhone(false, "+86(0)69445464");

assertValidPhone(false, "+1229 343433345");
}

public void assertValidPhone(Boolean valid, String phone){
Validation.clear();
ValidationResult result = Validation.phone("phone", phone);
assertEquals("Validation phone [" + phone + "] should be " + valid, valid, result.ok);
}

}

0 comments on commit 0a03bf7

Please sign in to comment.