Last active
January 20, 2021 20:49
-
-
Save kieranbarker/55a12cac034c386a5b3669b991290bf6 to your computer and use it in GitHub Desktop.
Check if a string is a valid email address
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
/** | |
* Check if a string is a valid email address | |
* {@link https://gist.github.com/kieranbarker/55a12cac034c386a5b3669b991290bf6} | |
* @param {String} str The string | |
* @returns {Boolean} Whether the string is a valid email address | |
*/ | |
function isValidEmail (str) { | |
// The regular expression used by [type="email"] | |
// https://html.spec.whatwg.org/multipage/input.html#valid-e-mail-address | |
const regex = /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/; | |
// Test the string against the regular expression | |
return regex.test(str); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment