Skip to content

Commit 2ef22ed

Browse files
DhanushNehruLacah
andauthored
Regex to check if the email format is valid or not (#683)
* Create readme.md * Add files via upload * Update readme.md Added a comment about the code only working in ES2021 --------- Co-authored-by: Laszlo <47461634+Lacah@users.noreply.github.com>
1 parent 480ba52 commit 2ef22ed

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
2+
3+
const email = "example@email.com";
4+
5+
if (emailRegex.test(email)) {
6+
console.log("Valid email address");
7+
} else {
8+
console.log("Invalid email address");
9+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
## Email Address Validation
2+
3+
This regular expression checks if the provided string matches the common pattern for email addresses.
4+
5+
**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.**
6+
7+
^[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.
8+
@: Matches the "@" symbol.
9+
[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.
10+
\.: Matches a dot.
11+
[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.

0 commit comments

Comments
 (0)