Skip to content

Commit

Permalink
Improve phone
Browse files Browse the repository at this point in the history
  • Loading branch information
nixonwidjaja committed Nov 5, 2023
1 parent 7c04adf commit d716e14
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
11 changes: 9 additions & 2 deletions src/main/java/seedu/address/model/person/Phone.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
*/
public class Phone {


public static final String MESSAGE_CONSTRAINTS =
"Phone numbers should only contain numbers, and it should be at least 3 digits long";
"Phone numbers should only contain numbers (or start with + sign for international numbers), " +
"and it should be between 3 - 20 digits long";
public static final String VALIDATION_REGEX = "\\d{3,}";
public final String value;

Expand All @@ -22,6 +22,7 @@ public class Phone {
*/
public Phone(String phone) {
requireNonNull(phone);
phone = phone.trim();
checkArgument(isValidPhone(phone), MESSAGE_CONSTRAINTS);
value = phone;
}
Expand All @@ -30,6 +31,12 @@ public Phone(String phone) {
* Returns true if a given string is a valid phone number.
*/
public static boolean isValidPhone(String test) {
if (test.length() < 3 || test.length() > 20) {
return false;
}
if (test.charAt(0) == '+') {
return test.substring(1).matches(VALIDATION_REGEX);
}
return test.matches(VALIDATION_REGEX);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

public class ParserUtilTest {
private static final String INVALID_NAME = " ";
private static final String INVALID_PHONE = "+651234";
private static final String INVALID_PHONE = "-651234";
private static final String INVALID_ADDRESS = " ";
private static final String INVALID_EMAIL = "example.com";
private static final String INVALID_TAG = "#friend";
Expand Down
4 changes: 4 additions & 0 deletions src/test/java/seedu/address/model/person/PhoneTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,15 @@ 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("+"));
assertFalse(Phone.isValidPhone("+99"));
assertFalse(Phone.isValidPhone("+1231231231231231321312331321312213213"));

// valid phone numbers
assertTrue(Phone.isValidPhone("911")); // exactly 3 numbers
assertTrue(Phone.isValidPhone("93121534"));
assertTrue(Phone.isValidPhone("124293842033123")); // long phone numbers
assertTrue(Phone.isValidPhone("+911"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

public class JsonAdaptedPersonTest {
private static final String INVALID_NAME = " ";
private static final String INVALID_PHONE = "+651234";
private static final String INVALID_PHONE = "-651234";
private static final String INVALID_ADDRESS = " ";
private static final String INVALID_EMAIL = "example.com";
private static final String INVALID_SALARY = "-1";
Expand Down

0 comments on commit d716e14

Please sign in to comment.