-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Create is valid email address algorithm #8907
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
Merged
tianyizheng02
merged 9 commits into
TheAlgorithms:master
from
CaedenPH:create-is-valid-email
Aug 14, 2023
Merged
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
dade6b9
feat(strings): Create is valid email address
CaedenPH c606c2f
updating DIRECTORY.md
b94f13c
feat(strings): Create is_valid_email_address algorithm
CaedenPH 15c5482
chore(is_valid_email_address): Implement changes from code review
CaedenPH 09fc6e8
Update strings/is_valid_email_address.py
CaedenPH bfd99cb
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 558aa54
chore(is_valid_email_address): Fix ruff error
CaedenPH 60ca06b
Merge branch 'create-is-valid-email' of https://github.com/caedenph/p…
CaedenPH bfec37c
Update strings/is_valid_email_address.py
CaedenPH File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
""" | ||
Implements an is valid email address algorithm | ||
|
||
@ https://en.wikipedia.org/wiki/Email_address | ||
""" | ||
|
||
import re | ||
import string | ||
|
||
email_tests: tuple[tuple[str, bool], ...] = ( | ||
("[email protected]", True), | ||
("[email protected]", True), | ||
("[email protected]", True), | ||
("[email protected]", True), | ||
("[email protected]", True), | ||
("[email protected]", True), | ||
("[email protected]", True), | ||
("[email protected]", True), | ||
("test/[email protected]", True), | ||
( | ||
"123456789012345678901234567890123456789012345678901234567890123@example.com", | ||
True, | ||
), | ||
("admin@mailserver1", True), | ||
("[email protected]", True), | ||
("Abc.example.com", False), | ||
("A@b@[email protected]", False), | ||
("[email protected]", False), | ||
("a(c)d,e:f;g<h>i[j\\k][email protected]", False), | ||
( | ||
"12345678901234567890123456789012345678901234567890123456789012345@example.com", | ||
False, | ||
), | ||
("i.like.underscores@but_its_not_allowed_in_this_part", False), | ||
) | ||
|
||
# The maximum octets (one character as a standard unicode character is one byte) | ||
# that the local part and the domain part can have | ||
MAX_LOCAL_PART_OCTETS = 64 | ||
MAX_DOMAIN_OCTETS = 255 | ||
|
||
|
||
def is_valid_email_address(email: str) -> bool: | ||
""" | ||
Returns True if the passed email address is valid. | ||
|
||
The local part of the email precedes the singular @ symbol and | ||
is associated with a display-name. For example, "john.smith" | ||
The domain is stricter than the local part and follows the @ symbol. | ||
|
||
Global email checks: | ||
1. There can only be one @ symbol in the email address. Technically if the | ||
@ symbol is quoted in the local-part, then it is valid, however this | ||
implementation ignores "" for now. | ||
(See https://en.wikipedia.org/wiki/Email_address#:~:text=If%20quoted,) | ||
2. The local-part and the domain are limited to a certain number of octets. With | ||
unicode storing a single character in one byte, each octet is equivalent to | ||
a character. Hence, we can just check the length of the string. | ||
Checks for the local-part: | ||
3. The local-part may contain: upper and lowercase latin letters, digits 0 to 9, | ||
and printable characters (!#$%&'*+-/=?^_`{|}~) | ||
4. The local-part may also contain a "." in any place that is not the first or | ||
last character, and may not have more than one "." consecutively. | ||
|
||
Checks for the domain: | ||
5. The domain may contain: upper and lowercase latin letters and digits 0 to 9 | ||
6. Hyphen "-", provided that it is not the first or last character | ||
7. The domain may also contain a "." in any place that is not the first or | ||
last character, and may not have more than one "." consecutively. | ||
|
||
>>> for email, valid in email_tests: | ||
... assert is_valid_email_address(email) is valid | ||
CaedenPH marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
|
||
# (1.) Make sure that there is only one @ symbol in the email address | ||
if email.count("@") != 1: | ||
return False | ||
|
||
local_part, domain = email.split("@") | ||
# (2.) Check octet length of the local part and domain | ||
if len(local_part) > MAX_LOCAL_PART_OCTETS or len(domain) > MAX_DOMAIN_OCTETS: | ||
return False | ||
|
||
# (3.) Validate the characters in the local-part | ||
if any( | ||
char not in string.ascii_letters + string.digits + ".(!#$%&'*+-/=?^_`{|}~)" | ||
for char in local_part | ||
): | ||
return False | ||
|
||
# (4.) Validate the placement of "." characters | ||
if ( | ||
local_part.startswith(".") | ||
or local_part.endswith(".") | ||
or re.search(r"\.\.+", local_part) | ||
): | ||
CaedenPH marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return False | ||
|
||
# (5.) Validate the characters in the domain | ||
if any(char not in string.ascii_letters + string.digits + ".-" for char in domain): | ||
return False | ||
|
||
# (6.) Validate the placement of "-" characters | ||
if domain.startswith("-") or domain.endswith("."): | ||
return False | ||
|
||
# (7.) Validate the placement of "." characters | ||
if domain.startswith(".") or domain.endswith(".") or re.search(r"\.\.+", domain): | ||
CaedenPH marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return False | ||
return True | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() | ||
|
||
for email, valid in email_tests: | ||
is_valid = is_valid_email_address(email) | ||
assert is_valid == valid, f"{email} is {is_valid}" | ||
print(f"Email address {email} is {'not ' if is_valid is False else ''}valid") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.