Blog / Tutorial

JavaScript Email Regex: A Simple Guide for Email Validation

Fuego Team /

Email validation is crucial whether you are a software engineer building an application or a marketer collecting user input. While there are numerous complex regular expressions (regex) available, a balanced approach often provides the most practical solution for validating email addresses. Keep in mind that regex only checks the format of an address. It can’t tell you who is behind it or whether it’s a throwaway.

Simple but Effective Email Regex Pattern

The key to a good regex for email validation is balancing complexity with effectiveness. Overly complex regex patterns can be expensive in terms of processing power and can be challenging to maintain. Below is a regex pattern that is simple enough to be fast but effective enough to catch most common email formats.

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

Breakdown of the Regex Pattern

  • ^: Asserts the position at the start of the string.
  • [^\s@]+: Matches one or more characters that are not whitespace or @.
  • @: Matches the @ symbol literally.
  • [^\s@]+: Matches one or more characters that are not whitespace or @.
  • \.: Matches the . symbol literally.
  • [^\s@]+: Matches one or more characters that are not whitespace or @.
  • $: Asserts the position at the end of the string.

This pattern is simple and quick. It focuses on ensuring that the email follows the basic structure: local-part@domain.

Code Example and Expected Output

Let’s see how to use this regex in JavaScript. Below is a function that checks if a given email is valid according to our regex.

function validateEmail(email) {
    const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
    return emailRegex.test(email);
}

// Test cases
console.log(validateEmail('[email protected]')); // true
console.log(validateEmail('john.doe.example.com')); // false
console.log(validateEmail('john.doe@com')); // false
console.log(validateEmail('john@[email protected]')); // false
console.log(validateEmail('john.doe@ example.com')); // false
console.log(validateEmail('[email protected]')); // true
console.log(validateEmail('[email protected]')); // true (see below)

Valid Email Addresses

Invalid Email Addresses

  • john.doe.example.com (missing @)
  • john.doe@com (no dot in the domain)
  • john@[email protected] (multiple @ symbols)
  • john.doe@ example.com (space after the @)

What Slips Through

[email protected] passes, even though consecutive dots can’t appear in a real domain. That’s the trade-off of a simple pattern: it checks the overall shape of the address, not every rule in the spec. Addresses like this are rare, and since no real domain is behind them, a DNS lookup on the domain will catch them.

Why Not Be Too Strict?

Being overly stringent with your regex pattern can reject valid but less common email formats. It could also lead to higher processing times and more complex code. For instance, emails can legally have a multitude of characters in their local parts and various top-level domains. Balancing performance with accuracy is key.

Beyond Regex: Know Who Just Signed Up

A regex only validates format. It can’t tell you that an address belongs to a disposable email service, that the domain has no DNS MX records, or that the signup came from a 58,000-person enterprise. That’s what Fuego’s lead enrichment API is for. One call, fast enough to run inside your signup flow, returns:

  • Segment: the organization behind the address and its category (enterprise, smb, education, government, nonprofit or individual), plus its name, industry, employee count and country
  • Block abuse: disposable, role and free flags, a DNS-only has_mx check, and a simple risk level
  • Fix typos: a did_you_mean suggestion for mistakes like gmail.cmo

Call it from your server so your API token stays private:

async function enrichEmail(email) {
  const params = new URLSearchParams({ email, token: process.env.FUEGO_KEY });
  const res = await fetch(`https://app.gofuego.io/api/v1/enrich?${params}`);
  return res.json();
}

const lead = await enrichEmail("[email protected]");
console.log(lead.category); // "enterprise"
console.log(lead.risk);     // "low"

In summary, a simple regex pattern is often “good enough” for client-side validation, ensuring that the basic structure is correct. To understand who’s actually signing up and block abuse, pair it with an enrichment API.

Start a free trial to try it out.

Segment your next signup in under 200ms

Start a free trial today. No survey, no scraped data, no lost signups.