Skip to content

Instantly share code, notes, and snippets.

@kieranbarker
Last active January 20, 2021 20:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kieranbarker/55a12cac034c386a5b3669b991290bf6 to your computer and use it in GitHub Desktop.
Save kieranbarker/55a12cac034c386a5b3669b991290bf6 to your computer and use it in GitHub Desktop.
Check if a string is a valid email address
/**
* 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