Skip to content

Commit

Permalink
Altered regex
Browse files Browse the repository at this point in the history
  • Loading branch information
remuslum committed Nov 9, 2023
1 parent f2deb98 commit e98ac63
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 20 deletions.
22 changes: 10 additions & 12 deletions src/main/java/seedu/address/model/person/Email.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
public class Email {

private static final String SPECIAL_CHARACTERS = "+_.-";
// alphanumeric and special characters
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 "
Expand All @@ -18,21 +24,13 @@ public class Email {
+ "2. This is followed by a '@' and then 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 at least 2 characters long\n"
+ " - have each domain label start and end with alphanumeric characters\n"
+ " - have each domain label consist of alphanumeric characters, separated only by hyphens, if any.";
// alphanumeric and special characters
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 + ")*";
private static final String DOMAIN_PART_REGEX = ALPHANUMERIC_NO_UNDERSCORE
+ "(-" + ALPHANUMERIC_NO_UNDERSCORE + ")*";
private static final String DOMAIN_LAST_PART_REGEX = "(" + DOMAIN_PART_REGEX + "){2,}$"; // At least two chars
private static final String DOMAIN_REGEX = "(" + DOMAIN_PART_REGEX + "\\.)*" + DOMAIN_LAST_PART_REGEX;
public static final String VALIDATION_REGEX = LOCAL_PART_REGEX + "@" + DOMAIN_REGEX;
+ " - 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";


public final String value;


/**
* Constructs an {@code Email}.
*
Expand Down
15 changes: 7 additions & 8 deletions src/test/java/seedu/address/model/person/EmailTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,27 +52,26 @@ public void isValidEmail() {
assertFalse(Email.isValidEmail("[email protected]")); // domain name starts with a hyphen
assertFalse(Email.isValidEmail("[email protected]")); // domain name ends with a hyphen
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

// valid email
assertTrue(Email.isValidEmail("[email protected]")); // underscore in local part
assertTrue(Email.isValidEmail("[email protected]")); // period in local part
assertTrue(Email.isValidEmail("[email protected]")); // '+' symbol in local part
assertTrue(Email.isValidEmail("[email protected]")); // hyphen in local part
assertTrue(Email.isValidEmail("a@bc")); // minimal
assertTrue(Email.isValidEmail("test@localhost")); // alphabets only
assertTrue(Email.isValidEmail("123@145")); // numeric local part and domain name
assertTrue(Email.isValidEmail("[email protected]")); // mixture of alphanumeric and special characters
assertTrue(Email.isValidEmail("[email protected]")); // long domain name
assertTrue(Email.isValidEmail("[email protected]")); // long local part
assertTrue(Email.isValidEmail("[email protected]")); // more than one period in domain
}

@Test
public void equals() {
Email email = new Email("valid@email");
Email email = new Email("valid@email.com");

// same values -> returns true
assertTrue(email.equals(new Email("valid@email")));
assertTrue(email.equals(new Email("valid@email.com")));

// same object -> returns true
assertTrue(email.equals(email));
Expand All @@ -84,7 +83,7 @@ public void equals() {
assertFalse(email.equals(5.0f));

// different values -> returns false
assertFalse(email.equals(new Email("other.valid@email")));
assertFalse(email.equals(new Email("other.valid@email.com")));
}

@Test
Expand Down

0 comments on commit e98ac63

Please sign in to comment.