Skip to content

Instantly share code, notes, and snippets.

@normanzb
Last active August 29, 2015 13:56
Show Gist options
  • Select an option

  • Save normanzb/8833200 to your computer and use it in GitHub Desktop.

Select an option

Save normanzb/8833200 to your computer and use it in GitHub Desktop.
email format very loose check
function validateEmail(email) {
// The real RFC complain email format check will be something look like this
// http://ex-parrot.com/~pdw/Mail-RFC822-Address.html
// so let's make a very loose check here to let go most cases
if ( typeof email != "string" ) {
return false;
}
var pstAt = email.indexOf('@');
// @ be the first char
if ( pstAt <= 0 ) {
return false;
}
// at least 1 char before and after the dot
else if ( pstAt > email.length - 4 ) {
return false
}
var domain = email.substr( pstAt + 1, email.length );
var pstDot = domain.indexOf('.');
if ( domain.indexOf('@') >= 0 ) {
// only one 'at'
return false;
}
if ( pstDot <= 0 ) {
// at least one dot behind the 'at'
// dot should not follow 'at' immediately
// Ps. dot at the last is allowed
return false;
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment