Skip to content

Instantly share code, notes, and snippets.

@SubZane
Created March 17, 2021 09:49
Show Gist options
  • Save SubZane/ddf031603d6876557ca8351ef8e320cb to your computer and use it in GitHub Desktop.
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.
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