Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add spaces phone validation #44

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/main/java/seedu/address/model/person/Phone.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class Phone {


public static final String MESSAGE_CONSTRAINTS =
"Phone numbers should only contain numbers, and it should be at least 3 digits long";
"Invalid phone number format. Enter a valid phone number that is more than 3 digits.";
public static final String VALIDATION_REGEX = "\\d{3,}";
public final String value;

Expand All @@ -21,9 +21,13 @@ public class Phone {
* @param phone A valid phone number.
*/
public Phone(String phone) {

requireNonNull(phone);
checkArgument(isValidPhone(phone), MESSAGE_CONSTRAINTS);
value = phone;
String trimmedPhone = phone.replaceAll(" ", "");
checkArgument(isValidPhone(trimmedPhone), MESSAGE_CONSTRAINTS);
value = trimmedPhone;


}

/**
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/seedu/address/model/person/PhoneTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,22 @@ public void constructor_invalidPhone_throwsIllegalArgumentException() {
assertThrows(IllegalArgumentException.class, () -> new Phone(invalidPhone));
}


@Test
public void constructor_validPhone_withSpaces() {
Phone phoneWithSpaces = new Phone(" 911 ");
assertTrue(phoneWithSpaces.equals(new Phone("911"))); // Should equal the trimmed version

Phone longPhoneWithSpaces = new Phone(" 93121534 ");
assertTrue(longPhoneWithSpaces.equals(new Phone("93121534"))); // Should equal the trimmed version

Phone mixedSpaces1 = new Phone(" 1234 5678 ");
assertTrue(mixedSpaces1.equals(new Phone("12345678")));

Phone mixedSpaces2 = new Phone(" 98 76 54 32 ");
assertTrue(mixedSpaces2.equals(new Phone("98765432")));
}

@Test
public void isValidPhone() {
// null phone number
Expand All @@ -31,6 +47,7 @@ public void isValidPhone() {
assertFalse(Phone.isValidPhone("phone")); // non-numeric
assertFalse(Phone.isValidPhone("9011p041")); // alphabets within digits
assertFalse(Phone.isValidPhone("9312 1534")); // spaces within digits
assertFalse(Phone.isValidPhone(" 9312 1534 ")); //spaces around digits

// valid phone numbers
assertTrue(Phone.isValidPhone("911")); // exactly 3 numbers
Expand Down