From 4c6817477f1a26cb02b98bc19d202dba5e2994d9 Mon Sep 17 00:00:00 2001 From: Afis Longe Date: Tue, 14 Oct 2025 01:26:14 +0000 Subject: [PATCH] refactor: update email validation regex to RFC 5322 compliant version --- .../Email Address Validation/README.md | 53 ++++++++++++++++--- .../Email Address Validation/isEmail.js | 27 +++++++--- 2 files changed, 65 insertions(+), 15 deletions(-) diff --git a/Specialized Areas/Regular Expressions/Email Address Validation/README.md b/Specialized Areas/Regular Expressions/Email Address Validation/README.md index d816fa8724..9dabbabb92 100644 --- a/Specialized Areas/Regular Expressions/Email Address Validation/README.md +++ b/Specialized Areas/Regular Expressions/Email Address Validation/README.md @@ -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"); +}); diff --git a/Specialized Areas/Regular Expressions/Email Address Validation/isEmail.js b/Specialized Areas/Regular Expressions/Email Address Validation/isEmail.js index 80acd850c3..c6a9cff069 100644 --- a/Specialized Areas/Regular Expressions/Email Address Validation/isEmail.js +++ b/Specialized Areas/Regular Expressions/Email Address Validation/isEmail.js @@ -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"); +}); \ No newline at end of file