diff --git a/src/main/java/seedu/address/model/person/Email.java b/src/main/java/seedu/address/model/person/Email.java index 2b06301eeff..e36cf65215d 100644 --- a/src/main/java/seedu/address/model/person/Email.java +++ b/src/main/java/seedu/address/model/person/Email.java @@ -14,18 +14,20 @@ public class Email { private static final String ALPHANUMERIC_NO_UNDERSCORE = "[^\\W_]+"; // alphanumeric characters except underscore private static final String LOCAL_PART_REGEX = "^" + ALPHANUMERIC_NO_UNDERSCORE + "([" + SPECIAL_CHARACTERS + "]" + ALPHANUMERIC_NO_UNDERSCORE + ")*"; - public static final String VALIDATION_REGEX = LOCAL_PART_REGEX + "@" + "([a-zA-Z]+\\.)+[a-zA-Z]{2,6}$"; - - public static final String MESSAGE_CONSTRAINTS = "Emails should be of the format local-part@domain " - + "and adhere to the following constraints:\n" - + "1. The local-part should only contain alphanumeric characters and these special characters, excluding " - + "the parentheses, (" + SPECIAL_CHARACTERS + "). The local-part may not start or end with any special " - + "characters.\n" - + "2. This is followed by a '@' and then a domain name. The domain name is made up of domain labels " + private static final String DOMAIN_REGEX = ALPHANUMERIC_NO_UNDERSCORE + "([" + SPECIAL_CHARACTERS + "]" + + ALPHANUMERIC_NO_UNDERSCORE + ")*"; + public static final String VALIDATION_REGEX = LOCAL_PART_REGEX + "@(" + DOMAIN_REGEX + "\\.)+[a-zA-Z]{2,6}$"; + + public static final String MESSAGE_CONSTRAINTS = "Emails should be of the format " + + "local-part@domain.top-level-domain and adhere to the following constraints:\n" + + "1. The local-part and domain should only contain alphanumeric characters and these special characters, " + + "excluding the parentheses, (" + SPECIAL_CHARACTERS + ").\n" + + "2. The local-part may not start or end with any special characters. Each special character cannot " + + "be next to each other.\n" + + "3. The local-part is followed by a '@' and a domain name. The domain name is made up of domain labels " + "separated by periods.\n" - + "The domain name must:\n" - + " - end with a domain label that is between 2 to 6 characters (e.g. .com, .edu)\n" - + " - have each domain label start and end with characters only\n"; + + "4. Each domain label start and end with alphanumeric characters only.\n" + + "5. The top-level-domain should only contain 2 to 6 alphabet characters (e.g., .com, .edu).\n"; public final String value; diff --git a/src/test/java/seedu/address/model/person/EmailTest.java b/src/test/java/seedu/address/model/person/EmailTest.java index 7847ebbaeb0..b2594128674 100644 --- a/src/test/java/seedu/address/model/person/EmailTest.java +++ b/src/test/java/seedu/address/model/person/EmailTest.java @@ -36,7 +36,6 @@ public void isValidEmail() { // invalid parts assertFalse(Email.isValidEmail("peterjack@-")); // invalid domain name - assertFalse(Email.isValidEmail("peterjack@exam_ple.com")); // underscore in domain name assertFalse(Email.isValidEmail("peter jack@example.com")); // spaces in local part assertFalse(Email.isValidEmail("peterjack@exam ple.com")); // spaces in domain name assertFalse(Email.isValidEmail(" peterjack@example.com")); // leading space @@ -54,8 +53,11 @@ public void isValidEmail() { assertFalse(Email.isValidEmail("peterjack@example.c")); // top level domain has less than two chars assertFalse(Email.isValidEmail("test@localhost")); // alphabets only assertFalse(Email.isValidEmail("123@145")); // numeric domain name - assertFalse(Email.isValidEmail("a1+be.d@example1.com")); // mixture of alphanumeric and special characters - assertFalse(Email.isValidEmail("peter_jack@very-very-very-long-example.com")); // domain name has symbols + assertFalse(Email.isValidEmail("asdf@gmail..com")); + assertFalse(Email.isValidEmail("asdf@gmail.co2")); + assertFalse(Email.isValidEmail("asdf@gmail_-s.com")); + assertFalse(Email.isValidEmail("as._df@x.com")); + assertFalse(Email.isValidEmail("asdf@asdf.asdfasdfsdf")); // valid email assertTrue(Email.isValidEmail("PeterJack_1190@example.com")); // underscore in local part @@ -64,6 +66,14 @@ public void isValidEmail() { assertTrue(Email.isValidEmail("PeterJack-1190@example.com")); // hyphen in local part assertTrue(Email.isValidEmail("if.you.dream.it_you.can.do.it@example.com")); // long local part assertTrue(Email.isValidEmail("e1234567@u.nus.edu")); // more than one period in domain + assertTrue(Email.isValidEmail("peterjack@exam_ple.com")); // underscore in domain name + assertTrue(Email.isValidEmail("a1+be.d@example1.com")); // mixture of alphanumeric and special characters + assertTrue(Email.isValidEmail("peter_jack@very-very-very-long-example.com")); // domain name has symbols + assertTrue(Email.isValidEmail("asdf@asdf2.ai")); + assertTrue(Email.isValidEmail("asdf@mercedes-benz.com")); + assertTrue(Email.isValidEmail("asdf@mercedes_benz.com")); + assertTrue(Email.isValidEmail("asdf_asdfasdf-asdfsFF@mercedes-benz_ccenz.com")); + assertTrue(Email.isValidEmail("gg@gg+asedf_google.com")); } @Test