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-emailSource 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
stringConverts 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.
capitalize
stringCapitalizes 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.
kebab-case
stringConverts 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.
pad-end
stringPads 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).
pad-start
stringPads 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).
pascal-case
stringConverts 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.
Quick Actions
Tags
Parameters
emailstringThe string to validate as an email
Returns
booleanTrue if the string is a valid email format, false otherwise