@toolz/looks-like-email

A utility function that determines whether a given string looks like a valid email address

Usage no npm install needed!

<script type="module">
  import toolzLooksLikeEmail from 'https://cdn.skypack.dev/@toolz/looks-like-email';
</script>

README

looks-like-email

looks-like-email is a utility function that determines whether a string looks like a valid email address. Obviously, this function cannot tell whether a string represents a working email address. But this utility performs a series of checks to ensure that the input at least appears to be a valid email address.

Usage

After installation, import the package:

import { looksLikeEmail } from '@toolz/looks-like-email';

looksLikeEmail()

Given any string, looksLikeEmail() performs the following validates that:

  1. The string contains exact one @ symbol.
  2. The @ symbol does not appear the beginning or end of the string. (The portion before the @ symbol is the "local part". The portion after it is the "domain part").
  3. The local part consists of alphanumerics and/or the following characters: ! # $ % & ‘ * + - / = ? ^ _ ` . { | } ~
  4. There are no consecutive periods in the local part.
  5. The domain part is no longer than 255 characters.
  6. There is some kind of top-level domain in the domain part. (Given the every fluctuating directory of TLDs, this utility does not try to validate that the supplied TLD is a real TLD.)
  7. The domain part is divided into domain labels by periods - and there are at least two domain labels.
  8. No domain label exceeds 63 characters in length.
  9. Each domain label consists of alphanumerics and/or a hyphen.
  10. Domain labels do not start or end with a hyphen.

This utility uses @toolz/string-contains to look for alphanumerics across the UTF-8 spectrum. This means that letters are accepted when they are outside the ASCII range.

const API = {
   arguments: {
      string: {
         required,
         format: string,
      },
      showWarnings: {
         optional,
         format: Boolean,
         defaultValue: false,
      },
   },
   returns: Boolean,
}

Examples:

looksLikeEmail('adam@bytebodger@foo.com'); // FALSE
looksLikeEmail('@adambytebodger.com'); // FALSE
looksLikeEmail('abcdefghijklmnopqrstuvwxzyabcdefghijklmnopqrstuvwxzyabcdefghijklmnopqrstuvwxzy@bytebodger.com'); // FALSE
looksLikeEmail('adambytebodger.com@'); // FALSE
looksLikeEmail('ad[am@bytebodger.com'); // FALSE
looksLikeEmail('ad..am@bytebodger.com'); // FALSE
looksLikeEmail('abc@defghijklmnopqrstuvwxzyabcdefghijklmnopqrstuvwxzyabcdefghijklmnopqrstuvwxzydefghijklmnopqrstuvwxzyabcdefghijklmnopqrstuvwxzyabcdefghijklmnopqrstuvwxzydefghijklmnopqrstuvwxzyabcdefghijklmnopqrstuvwxzyabcdefghijklmnopqrstuvwxzydefghijklmnopqrstuvwxzyabcdefghijklmnopqrstuvwxzyabcdefghijklmnopqrstuvwxzydefghijklmnopqrstuvwxzyabcdefghijklmnopqrstuvwxzyabcdefghijklmnopqrstuvwxzydefghijklmnopqrstuvwxzyabcdefghijklmnopqrstuvwxzyabcdefghijklmnopqrstuvwxzydefghijklmnopqrstuvwxzyabcdefghijklmnopqrstuvwxzyabcdefghijklmnopqrstuvwxzydefghijklmnopqrstuvwxzyabcdefghijklmnopqrstuvwxzyabcdefghijklmnopqrstuvwxzydefghijklmnopqrstuvwxzyabcdefghijklmnopqrstuvwxzyabcdefghijklmnopqrstuvwxzydefghijklmnopqrstuvwxzyabcdefghijklmnopqrstuvwxzyabcdefghijklmnopqrstuvwxzydefghijklmnopqrstuvwxzyabcdefghijklmnopqrstuvwxzyabcdefghijklmnopqrstuvwxzydefghijklmnopqrstuvwxzyabcdefghijklmnopqrstuvwxzyabcdefghijklmnopqrstuvwxzydefghijklmnopqrstuvwxzyabcdefghijklmnopqrstuvwxzyabcdefghijklmnopqrstuvwxzydefghijklmnopqrstuvwxzyabcdefghijklmnopqrstuvwxzyabcdefghijklmnopqrstuvwxzydefghijklmnopqrstuvwxzyabcdefghijklmnopqrstuvwxzyabcdefghijklmnopqrstuvwxzydefghijklmnopqrstuvwxzyabcdefghijklmnopqrstuvwxzyabcdefghijklmnopqrstuvwxzydefghijklmnopqrstuvwxzyabcdefghijklmnopqrstuvwxzyabcdefghijklmnopqrstuvwxzydefghijklmnopqrstuvwxzyabcdefghijklmnopqrstuvwxzyabcdefghijklmnopqrstuvwxzybytebodger.com'); // FALSE
looksLikeEmail('adam@bytebodgercom'); // FALSE
looksLikeEmail('adam@bytebodgerbytebodgercombytebodgercombytebodgercombytebodgercombytebodgercombytebodgercombytebodgercom.com'); // FALSE
looksLikeEmail('adam@byte*bodger.com'); // FALSE
looksLikeEmail('adam@byte.-bodger.com'); // FALSE
looksLikeEmail('adam@byte-.bodger.com'); // FALSE
looksLikeEmail('adam@byte-.bodger.com', true); // FALSE (with console warning about the error)
looksLikeEmail('adam@byte.bodger.com'); // TRUE
looksLikeEmail('adam_davis1@byte.bodger.com'); TRUE