Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nepsilon/a94f77c3a92f9149a04aca48a1885414 to your computer and use it in GitHub Desktop.
Save nepsilon/a94f77c3a92f9149a04aca48a1885414 to your computer and use it in GitHub Desktop.
Email address syntax validation? Let the browser do it (without regexp) — First published in fullweb.io issue #77

Email address syntax validation? Let the browser do it (without regexp)

Here is a simple and robust way to check for the validity of an email address syntax directly in the browser. No need for crazy regular expressions.

e = document.createElement('input')
e.type = 'email'
// check some email addresses
e.value = 'hi@'
e.validity.valid
// false
e.value = '[email protected]'
e.validity.valid
//true

You can go further and explore the HTML5 email input element for more functionality, using the pattern attribute for a custom regexp, checking for too long/short or empty value and more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment