Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,48 @@
## Email Address Validation
# Email Address Validation

This regular expression checks if the provided string matches the common pattern for email addresses.
This project provides email validation functionality in pure JavaScript. It includes a refactored regex implementation that covers common RFC 5322 patterns and stricter domain rules.

**Please note that the code is based on ES2021, and as such, will not work in the global scope or scopes that are not ES2021 compatible.**
---

^[a-zA-Z0-9._%+-]+: Matches one or more characters that can be letters (both uppercase and lowercase), digits, dots, underscores, percent signs, or plus or hyphen signs at the start of the string.
@: Matches the "@" symbol.
[a-zA-Z0-9.-]+: Matches one or more characters that can be letters, digits, dots, or hyphens in the domain part of the email address.
\.: Matches a dot.
[a-zA-Z]{2,}$: Matches two or more letters at the end of the string, representing the top-level domain (TLD) of the email address.
## Refactored Email Validation (2025 Update)

The regex has been improved to handle edge cases in the local part, domain, and top-level domain (TLD).

### Key Features

- Supports letters, digits, and allowed special characters in the local part:
`!#$%&'*+/=?^_`{|}~-`
- Supports dots in the local part, but not consecutive dots.
- Supports quoted local parts, e.g., `"john.doe"@example.com`.
- Validates domain labels:
- Letters, digits, and hyphens (`-`)
- Labels cannot start or end with a hyphen
- No consecutive dots
- Restricts TLD length to 2–63 characters.
- Supports IPv4/IPv6 literals in brackets, e.g., `user@[192.168.0.1]`.

### Example Usage

```js
const emailRegex = /^(?:[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:(?:\\[\x00-\x7f]|[^\\"\r\n])*)")@(?:(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,63}|\[(?:(?:25[0-5]|2[0-4][0-9]|1?[0-9]{1,2})\.){3}(?:25[0-5]|2[0-4][0-9]|1?[0-9]{1,2}|[a-zA-Z0-9-]*[a-zA-Z0-9]:[^\]]+)\])$/;

function validateEmail(email) {
return emailRegex.test(email);
}

const emails = [
"example@email.com",
"user.name+tag@example.co.uk",
'"quoted.user"@example.com',
"user@[192.168.1.1]",
"user@-example.com",
"user@example..com",
"user@example-.com",
"user@.example.com"
];

emails.forEach(email => {
console.log(email, validateEmail(email)
? "is valid"
: "is invalid");
});
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
//const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;

const email = "example@email.com";
//refactor emailRegex
const emailRegex = /^(?:[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:(?:\\[\x00-\x7f]|[^\\"\r\n])*)")@(?:(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,63}|\[(?:(?:25[0-5]|2[0-4][0-9]|1?[0-9]{1,2})\.){3}(?:25[0-5]|2[0-4][0-9]|1?[0-9]{1,2}|[a-zA-Z0-9-]*[a-zA-Z0-9]:[^\]]+)\])$/;

if (emailRegex.test(email)) {
console.log("Valid email address");
} else {
console.log("Invalid email address");
}
const emails = [
// Valid emails
"example@email.com",
"user.name+tag@example.co.uk",
'"quoted.user"@example.com',
"user@[192.168.1.1]",

// Invalid emails (should fail)
"user@-example.com",
"user@example..com",
"user@example-.com",
"user@.example.com"
];

emails.forEach(email => {
console.log(email, emailRegex.test(email) ? "is valid email addres" : "is invalid email address");
});
Loading