Skip to content
Blog

Regular Expression for Email Validation in JavaScript

By Published A short read

A practical regular expression for email validation in JavaScript is /^[^\s@]+@[^\s@]+\.[^\s@]+$/: some text, one @, then a domain with a dot, and no spaces. It catches typos, not fake addresses. In a form, let the browser check with <input type="email">, use a library on the server, and send a confirmation email: that's the only real test.

In this post we fix the pattern from our 2022 post and video (it let brackets through and turned away .info addresses), build a simple pattern you can read, test it and three other checks against 15 addresses, and see what no regex can tell you.

Key takeaways

  • No regex can tell you that an address exists or belongs to the person typing it. Only a confirmation email, with a link they click, proves that.
  • The simple pattern /^[^\s@]+@[^\s@]+\.[^\s@]+$/ catches typos like a missing @ or a space, and in our tests it let every valid address through. It also lets some invalid ones through, and for a typo check that's fine.
  • [A-z] is a classic bug. Between Z and a sit six more characters: [ \ ] ^ _ and the backtick. Write [A-Za-z].
  • Don't limit the ending to 2 or 3 letters: .info, .email and .museum are real. 973 of the 1,437 top-level domains IANA lists are longer than three characters.
  • In a form, <input type="email"> checks the format for free. On the server, use a well-tested library such as validator.js.

Our 2022 video builds the original pattern and shows how test() works. Watch it for that, then read on: the pattern itself had bugs, and this post fixes them.

What was wrong with our 2022 regex?

Here's the pattern from the original post, tested on four addresses:

const oldRegex = /^[_A-z0-9-]+(\.[_A-z0-9]+)*@[A-z0-9]+(\.[A-z0-9-]+)*(\.[A-z]{2,3})$/;

console.log(oldRegex.test("asha[1]@example.com"));   // true, but [ and ] aren't allowed there
console.log(oldRegex.test("hello@letsbug.info"));    // false, but .info is a real ending
console.log(oldRegex.test("asha+news@example.com")); // false, but + is allowed
console.log(oldRegex.test("asha@my-site.com"));      // false, but - is allowed in domains

Four separate bugs:

  • [A-z] isn't "all letters". In a character class, a range covers every character whose code is between the two ends. A is 65, Z is 90, a is 97 and z is 122. Codes 91 to 96 are [, \, ], ^, _ and the backtick, so [A-z] lets all six through.
  • {2,3} rejects longer endings. The pattern demands that the address ends in a dot and 2 or 3 letters. IANA's list of top-level domains (20 Oct 2026) has 1,437 entries, and 973 of them are longer than three characters: .info, .email, .museum, .photography.
  • No + or ' before the @. The email message-format standard, RFC 5322, allows letters, digits and ! # $ % & ' * + - / = ? ^ _ ` { | } ~ there. asha+news@example.com and o'brien@example.ie are real addresses.
  • No hyphen in the first part of the domain. [A-z0-9]+ right after the @ has no -, so my-site.com fails, even though mail.my-site.com would pass.

What is a good regex for email validation?

Instead of trying to describe every valid address, check the shape: something, an @, something, a dot, something.

const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;

function looksLikeEmail(text) {
  return emailPattern.test(text.trim());
}

console.log(looksLikeEmail("asha@example.com"));    // true
console.log(looksLikeEmail("  asha@example.com ")); // true, the spaces are trimmed first
console.log(looksLikeEmail("asha.example.com"));    // false, no @
console.log(looksLikeEmail("asha@example"));        // false, no dot after the @
console.log(looksLikeEmail("asha @example.com"));   // false, a space

Piece by piece:

  • ^ and $ make the whole string match, not just a piece of it.
  • [^\s@]+ means one or more characters that are not whitespace (\s) and not @. The ^ inside the brackets means "not".
  • @ is the at sign. Since the parts on either side can't contain another @, there's exactly one.
  • \. is a real dot. A plain . in a regex means "any character".

It's deliberately loose. asha@example..com passes, and so does asha@example.c0m. That's the trade: a typo check that turns away almost no real addresses, with the real test (an email they have to open) still to come. The rare exceptions are addresses with no dot after the @, and quoted names with a space in them, like "asha rao"@example.com, which the standard allows but almost nobody uses.

15 addresses, four checks

Here's how the 2022 pattern, the simple pattern, the browser's type="email" check (Chrome 155) and the validator.js library (13.15.35, in Node.js 24.20.0) handle 15 addresses. The first seven are valid. The last eight are not, for the reason given.

AddressReally valid?2022 regexSimple patterntype="email"validator.isEmail
asha@example.comYesYesYesYesYes
Asha.Rao@mail.example.co.inYesYesYesYesYes
asha+news@example.comYesNoYesYesYes
hello@letsbug.infoYesNoYesYesYes
asha@example.museumYesNoYesYesYes
asha@my-site.comYesNoYesYesYes
o'brien@example.ieYesNoYesYesYes
asha[1]@example.comNo: [ ] need quotesYesYesNoNo
asha@my_site.comNo: no _ in domainsYesYesNoNo
asha@exa^mple.comNo: no ^ in domainsYesYesNoNo
asha@@example.comNo: two @NoNoNoNo
asha @example.comNo: a spaceNoNoNoNo
asha@example..comNo: two dots in a rowNoYesNoNo
asha..rao@example.comNo: two dots in a rowNoYesYesNo
asha@-example.comNo: can't start with -NoYesNoNo

Before the @, "really valid" follows RFC 5322. After it, the domain has to be a real host name: letters, digits and hyphens, with no hyphen at the start or end of a part. The HTML standard takes that rule from RFC 1034. (RFC 5322's own grammar is looser here, which is why it isn't enough on its own.)

What the table shows:

  • The 2022 regex is wrong 8 times out of 15, and it's the worst kind of wrong: it turns away five real addresses.
  • The simple pattern accepts all seven valid addresses and lets six invalid ones through. As a typo check, that's the right side to err on.
  • The browser gets 14 right. It still accepts asha..rao@example.com.
  • validator.isEmail gets all 15 right here. It still can't tell you whether the mailbox exists.

How do you validate an email in HTML? Use type="email"

In a form, you don't need a regex at all. Give the input type="email" and the browser checks the format before the form is sent and tells the user what's wrong:

<form>
  <label for="email">Email</label>
  <input type="email" id="email" name="email" autocomplete="email" required>
  <button>Sign up</button>
</form>

You can ask the browser for its answer in JavaScript too:

const email = document.querySelector("#email");

email.value = "asha@my_site.com";
console.log(email.checkValidity());       // false
console.log(email.validity.typeMismatch); // true, it isn't an email address

email.value = "  asha@example.com ";
console.log(email.checkValidity());       // true
console.log(JSON.stringify(email.value)); // "asha@example.com", the spaces are gone

Browsers follow the HTML standard's definition of a valid email address, and the standard also gives it as a regular expression. Use it when you want the same rule somewhere else, in Node.js for example:

const htmlEmailPattern = /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;

console.log(htmlEmailPattern.test("asha+news@example.com")); // true
console.log(htmlEmailPattern.test("asha@my_site.com"));      // false
console.log(htmlEmailPattern.test("asha@example"));          // true, no dot needed

The standard bends the email rules on purpose. It calls its definition "a willful violation of RFC 5322", because the full RFC syntax allows comments, spaces and quoted strings that hardly anyone uses. It also accepts asha@example, with no dot after the @, because its grammar lets a domain be a single part (like a computer's name on a local network). If you need a dot, add your own check. And it only accepts plain ASCII before the @: josé@example.com fails, even though some mail systems can deliver to it.

The browser check only protects honest users from typos. Anyone can turn it off or send the form without a browser. MDN says "your site must not use this validation for any security purposes". Always check again on the server.

How do you validate an email on the server?

OWASP's advice on email validation is short: "Use well-tested libraries instead of custom regex", because "Strict regex-based validation often rejects valid addresses or introduces inconsistencies." In Node.js, the usual choice is validator.js:

npm install validator@13.15.35
import validator from "validator";

console.log(validator.isEmail("asha@example.com"));      // true
console.log(validator.isEmail("asha..rao@example.com")); // false
console.log(validator.isEmail("asha@example"));          // false, it needs a dot by default

It has options for the cases a regex can't sensibly cover, such as display names like Asha <asha@example.com>. Our Node.js JWT authentication API checks emails with isEmail() from express-validator in the same way.

How do you know an email address is real? Send a confirmation email

Every check so far looks at the shape of the text. None of them can tell you whether asha@example.com has a mailbox behind it, or whether it belongs to the person filling in your form. As OWASP puts it: "Format validation does not prove mailbox access." The way to find out is to send an email and see if they open it:

  1. Save the address as not confirmed yet.
  2. Make a random token. In Node.js, crypto.randomBytes(32).toString("hex") gives 64 random hex characters. Store a hash of it (SHA-256 is fine for a long random token) with an expiry time, so a leaked database doesn't hand out working links.
  3. Email a link that contains the token itself.
  4. When the link is opened, hash the token from the link and look for that hash. Check it hasn't expired or been used, then mark the address confirmed and delete the stored hash.

OWASP's guidance on email verification asks for exactly this: tokens that are random, single-use and time-limited, and no account activation before the address is verified.

Common mistakes

Adding the g flag

const withG = /^[^\s@]+@[^\s@]+\.[^\s@]+$/g;

console.log(withG.test("asha@example.com")); // true
console.log(withG.test("asha@example.com")); // false, the same address!

With the g flag, test() remembers where it stopped (lastIndex) and starts the next search from there. For checking a whole value, leave g off.

Forgetting ^ and $

const noAnchors = /[^\s@]+@[^\s@]+\.[^\s@]+/;

console.log(noAnchors.test("my email is asha@example.com, thanks")); // true

Without ^ and $, the pattern only needs to find an email-shaped piece somewhere in the text.

Not trimming

People paste addresses with a space at the end. The browser strips leading and trailing spaces from type="email" for you, but a regex doesn't, so call trim() first, as looksLikeEmail() does.

Comparing addresses without a rule

The domain part is never case-sensitive, so Example.COM and example.com are the same. The part before the @ is, according to the rules, even though many providers ignore case. OWASP suggests keeping the address as typed and choosing one comparison rule for your whole app. Lower-casing the whole address for comparison is a fine rule if you pick it on purpose and use it everywhere: sign-up, login and password reset. Our JWT API post does exactly that.

Answer these first, then check them in your browser console.

1) Does the simple pattern accept asha@example.c0m? Why?
2) Which of the four checks in the table accept asha@example?
3) Fix /^[A-z]+$/ so it accepts only letters. What do the old and fixed versions say about "A_b"?

Show the answers

1) Yes. It only checks the shape (text, @, text, dot, text). Whether c0m is a real ending isn't its job, and a confirmation email would catch it.
2) Only type="email" (and the HTML standard's regex). The 2022 regex, the simple pattern and validator.isEmail all want a dot after the @.
3) /^[A-Za-z]+$/. The old one says true for "A_b", because _ is code 95, between Z and a. The fixed one says false.

Questions people ask

What is the best regex for email validation in JavaScript?

For a typo check, /^[^\s@]+@[^\s@]+\.[^\s@]+$/: text, one @, text, a dot, text, no spaces. It's short, readable and rejects almost no real addresses. For something stricter, use the HTML standard's regex (what type="email" checks) or a library such as validator.js. No regex proves an address exists.

Why is [A-z] wrong in a regex?

A range covers every character whose code lies between the two ends. A is 65 and z is 122, and in between, after Z (90), come [ \ ] ^ _ and the backtick (91 to 96). So [A-z] matches those six symbols too. Use [A-Za-z].

Does input type="email" validate email addresses?

It checks the format, following the HTML standard, before the form is sent. It doesn't check that the address exists, and anyone can bypass it, so you still need a check on the server and a confirmation email.

How do I check if an email address really exists?

Send it a message with a link that holds a random, single-use token that expires, and wait for the person to click it. Until they do, treat the address as unconfirmed.

Should I allow + in email addresses?

Yes. RFC 5322 allows + before the @, so an address like name+shop@example.com is valid, and blocking it turns away real users. The browser and validator.js both accept it.

Can I use the same check in Node.js and the browser?

Yes. Copy the HTML standard's regex from this post to get the same rule as type="email", or use validator.js, which works in both. Keep the server check even if the browser already checked.

Keep going

Checked on 20 October 2026 against the 9 sources below.

Sources

Every pattern in this post was run against all 15 addresses in Node.js 24.20.0 and Chrome 155 before publishing.