Created
March 17, 2021 09:49
-
-
Save SubZane/ddf031603d6876557ca8351ef8e320cb to your computer and use it in GitHub Desktop.
Validates Email address against regular expression and a list of "allowed" email domains.
This file contains 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
export function isValidEmail(email: string, acceptedDomains?: string[]) { | |
const emailformat = /^(([^<>()[\]\.,;:\s@\"]+(\.[^<>()[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i | |
let isValid = false | |
if (email.match(emailformat)) { | |
isValid = true | |
if (acceptedDomains !== undefined) { | |
isValid = acceptedDomains.some(function(domain) { | |
return email.endsWith(domain) | |
}) | |
} | |
} else { | |
isValid = false | |
} | |
return isValid | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment