Skip to content

Instantly share code, notes, and snippets.

@utilmind
Created July 28, 2021 22:33
Show Gist options
  • Save utilmind/be9795d18d50fba104028a374fbb49bf to your computer and use it in GitHub Desktop.
Save utilmind/be9795d18d50fba104028a374fbb49bf to your computer and use it in GitHub Desktop.
isValidEmail() JavaScript
// see also is_valid_email() in "strings.php".
String.prototype.isValidEmail = function() {
// This all are valid accordingly to RFC: !#$%&'*+-/=?^_`{|}~
// Gmail use + for subadressing. Usage of other special chars in unknown, but they are still valid anyway.
// Double-dot, however (..) is not allowed.
return 0 <= this.indexOf("..")
? false // email can't have 2 dots at row
: /^([\w!#$%&'*+\-/=?^_`{|}~]+(?:\.[\w!#$%&'*+\-/=?^_`{|}~]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,30}(?:\.[a-z]{2})?)$/i.test(this.trim()); // the longest domain extension in 2015 was ".cancerresearch", and looks like it's not the limit. UPD. how about .travelersinsurance? I set up it the longest domain extension to 30 chars.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment