Skip to content

Instantly share code, notes, and snippets.

@vnykmshr
Last active December 25, 2015 14:09
Show Gist options
  • Save vnykmshr/6988750 to your computer and use it in GitHub Desktop.
Save vnykmshr/6988750 to your computer and use it in GitHub Desktop.
Simple Email Validations
/**
* Simple email validation
* ensures at least one `@` sign, at least one char in local part,
* at least one `.` in domain part and is at least one char long
*/
isEmail: function (em) {
em = em || '';
var indx = em.lastIndexOf('@');
return indx > 0 && (em.lastIndexOf('.') > indx) && (em.length - indx > 1);
}
module.exports = isEmail;
@saich
Copy link

saich commented Oct 16, 2013

This might work better, and closer to HTML5 email validation:

var pattern = /^[a-zA-Z0-9._\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,4}$/g;
return pattern.test(email);

Source: https://github.com/dsheiko/HTML5-Form-Shim/blob/master/js/source/Input/Email.js

@vnykmshr
Copy link
Author

Yes, your regex solution will work, and I would prefer applying it in forms to validate email addresses. I have written the simple validation routine just to avoid the most blatant ones.

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