Skip to content

Commit

Permalink
Merge pull request #194 from nixonwidjaja/master
Browse files Browse the repository at this point in the history
Improve email regex
  • Loading branch information
remuslum committed Nov 12, 2023
2 parents 52321df + d2ad782 commit a2501de
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
24 changes: 13 additions & 11 deletions src/main/java/seedu/address/model/person/Email.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 "
+ "[email protected] 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;
Expand Down
16 changes: 13 additions & 3 deletions src/test/java/seedu/address/model/person/EmailTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 [email protected]")); // spaces in local part
assertFalse(Email.isValidEmail("peterjack@exam ple.com")); // spaces in domain name
assertFalse(Email.isValidEmail(" [email protected]")); // leading space
Expand All @@ -54,8 +53,11 @@ public void isValidEmail() {
assertFalse(Email.isValidEmail("[email protected]")); // 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("[email protected]")); // mixture of alphanumeric and special characters
assertFalse(Email.isValidEmail("[email protected]")); // domain name has symbols
assertFalse(Email.isValidEmail("[email protected]"));
assertFalse(Email.isValidEmail("[email protected]"));
assertFalse(Email.isValidEmail("asdf@gmail_-s.com"));
assertFalse(Email.isValidEmail("[email protected]"));
assertFalse(Email.isValidEmail("[email protected]"));

// valid email
assertTrue(Email.isValidEmail("[email protected]")); // underscore in local part
Expand All @@ -64,6 +66,14 @@ public void isValidEmail() {
assertTrue(Email.isValidEmail("[email protected]")); // hyphen in local part
assertTrue(Email.isValidEmail("[email protected]")); // long local part
assertTrue(Email.isValidEmail("[email protected]")); // more than one period in domain
assertTrue(Email.isValidEmail("peterjack@exam_ple.com")); // underscore in domain name
assertTrue(Email.isValidEmail("[email protected]")); // mixture of alphanumeric and special characters
assertTrue(Email.isValidEmail("[email protected]")); // domain name has symbols
assertTrue(Email.isValidEmail("[email protected]"));
assertTrue(Email.isValidEmail("[email protected]"));
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
Expand Down

0 comments on commit a2501de

Please sign in to comment.