← Back to utilities
string

is-email

Validates if a string is a valid email address. Uses a practical regex pattern that covers most common email formats while avoiding overly complex RFC-strict validation. Handles international characters in the local part and common TLDs.

Installation

npx fragmen add string/is-email

Source Code

/**
 * Validates if a string is a valid email address.
 *
 * Uses a practical regex pattern that covers most common email formats
 * while avoiding overly complex RFC-strict validation. Handles international
 * characters in the local part and common TLDs.
 *
 * @tags validation, string-manipulation
 * @param {string} email The string to validate as an email
 * @returns {boolean} True if the string is a valid email format, false otherwise
 *
 * @example
 * ```typescript
 * isEmail('user@example.com'); // true
 * isEmail('user.name+tag@example.co.uk'); // true
 * isEmail('invalid.email'); // false
 * isEmail('missing@domain'); // false
 * isEmail('@example.com'); // false
 * isEmail(''); // false
 * ```
 */
export function isEmail(email: string): boolean {
  if (typeof email !== 'string' || email.trim() === '') {
    return false;
  }

  // Practical email regex that covers most common cases
  const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;

  // Basic validation
  if (!emailRegex.test(email)) {
    return false;
  }

  // Additional checks
  const [local, domain] = email.split('@');

  // Local part (before @) constraints
  if (local.length > 64 || local.startsWith('.') || local.endsWith('.')) {
    return false;
  }

  // Domain part (after @) constraints
  if (domain.length > 255) {
    return false;
  }

  // Check each label in domain for hyphen constraints
  const domainLabels = domain.split('.');
  for (const label of domainLabels) {
    if (label.startsWith('-') || label.endsWith('-')) {
      return false;
    }
  }

  return true;
}

Examples

isEmail('user@example.com'); // true
isEmail('user.name+tag@example.co.uk'); // true
isEmail('invalid.email'); // false
isEmail('missing@domain'); // false
isEmail('@example.com'); // false
isEmail(''); // false

Related Utilities

camel-case

string

Converts a string to camelCase. Transforms a string by removing spaces, underscores, and hyphens, then capitalizing the first letter of each word except the first one. Commonly used for JavaScript variable names, object properties, and function names.

#pure#string-manipulation#formatting

capitalize

string

Capitalizes the first letter of a string. Converts the first character to uppercase while leaving the rest unchanged. Safely handles edge cases like empty strings and non-string inputs.

#pure#string-manipulation#formatting

kebab-case

string

Converts a string to kebab-case. Transforms a string by converting it to lowercase and replacing spaces, underscores, and camelCase boundaries with hyphens. Useful for creating URL slugs, CSS class names, and file names.

#pure#string-manipulation#formatting

pad-end

string

Pads the end of a string with another string until it reaches the target length. If padString is '', the original string is returned (mirrors native behavior).

#pure#string-manipulation#formatting

pad-start

string

Pads the start of a string with another string until it reaches the target length. If padString is '', the original string is returned (mirrors native behavior).

#pure#string-manipulation#formatting

pascal-case

string

Converts a string to PascalCase. Transforms a string by removing spaces, underscores, and hyphens, then capitalizing the first letter of each word including the first one. Commonly used for class names, interface names, type names, and component names.

#pure#string-manipulation#formatting

Quick Actions

Estimated size:1.53 KB

Tags

Parameters

emailstring

The string to validate as an email

Returns

boolean

True if the string is a valid email format, false otherwise