Skip to content

Instantly share code, notes, and snippets.

@madrobby
Forked from 140bytes/LICENSE.txt
Created May 19, 2011 12:06
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save madrobby/980601 to your computer and use it in GitHub Desktop.
Save madrobby/980601 to your computer and use it in GitHub Desktop.
Email validation

Check a string if it resembles an email address.

var isEmail = function(a){return/^[\w\.%\+\-]+@(?:[A-Z0-9\-]+\.)+(?:[A-Z]{2,6})$/i.test(a)};

isEmail('thomas@fesch.at'); // => true

This regexp was lifted from an early version of the restful authentication Rails plugin, available from https://github.com/technoweenie/restful-authentication and available under a MIT license, Copyright (c) 2009 Rick Olson.

function(
a // potential email adress to check
){
return
// /
// ^ start of string
// [\w\.%\+\-]+ name/string before the @
// @ the @
// (?:[A-Z0-9\-]+\.)+ multiple subdomains
// (?:[A-Z]{2,6}) top-level domain
// $ end of string
// /i case insensitive
/^[\w\.%\+\-]+@(?:[A-Z0-9\-]+\.)+(?:[A-Z]{2,6})$/i
.test(a) // test of email matches the regexp
}
function(a){return/^[\w\.%\+\-]+@(?:[A-Z0-9\-]+\.)+(?:[A-Z]{2,6})$/i.test(a)}
Copyright (c) 2011 Thomas Fuchs, http://mir.aculo.us
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
{
"name": "check_email",
"description": "Check a string if it resembles an email address.",
"keywords": [
"form",
"email",
"validation"
]
}
@maettig
Copy link

maettig commented Nov 11, 2011

I found this by surfing the examples at 140byt.es. It's possible to shorten your regular expression without changing anything. Simply omit most of the backslashes (not needed inside character classes) and unused brackets.

function(a){return/^[\w.%+-]+@([A-Z\d-]+\.)+[A-Z]{2,6}$/i.test(a)}

Please note that this validation (similar to almost all email validations you can find on the web) reject many valid email addresses. For example, it rejects international domain names like tüv.de. I suggest reading this very good article from the Linux Journal: Validate an E-Mail Address with PHP, the Right Way.

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