I don't have to use a regex if I use the methodology I specified.
Simple Java implementation off the top of my head. Very fast, no imports or expression compilation required:
bool isValidEmailAddress(String emailAddress) { int at = emailAddress.indexOf('@'); if (at < 1 || at == emailAddress.length() - 1) return false; return !Character.isWhiteSpace(emailAddress.charAt(at - 1)) && !Character.isWhiteSpace(emailAddress.charAt(at + 1)); }
int is_valid_email(char *email) { char *at = strstr(email, "@"); if (at <= email || at == strlen(email) + at - 2) return 0; return !isspace(*(at - 1)) && !isspace(*(at + 1)); }
assert(0 == is_valid_email("")); assert(0 == is_valid_email("@b")); assert(0 == is_valid_email("b@")); assert(0 == is_valid_email("d@ ")); assert(0 == is_valid_email(" @d")); assert(0 == is_valid_email(" ")); assert(1 == is_valid_email("a@b")); assert(1 == is_valid_email("John Smith <x.y@z.com>"));
the goal of client-side validation is to ensure that you can actually make that network call to do a real validation. the rfc is so complicated it's not even worth getting into this business, as evidenced by op's regex.
would love to see some unit tests for that thing.
I don't have to use a regex if I use the methodology I specified.
Simple Java implementation off the top of my head. Very fast, no imports or expression compilation required:
Improvements welcome. Should be portable to any other language trivially.