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

Improve email regex #194

Merged
merged 1 commit into from
Nov 12, 2023
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
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
Loading