Blog / Tutorial
JavaScript Email Regex: A Simple Guide for Email Validation
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: Verify the Address
A regex only checks format. It can’t tell you whether the mailbox actually exists, whether the domain accepts every address you throw at it, or whether the address is a disposable throwaway. Fuego’s email verification API answers those questions with a real SMTP check, and includes domain intelligence about the company behind the address at no extra cost:
- Verify: a
resultofdeliverable,undeliverable,riskyorunknown, plus areasonlikeaccepted_emailorinvalid_domain - Flag risky signups:
disposable,role,freeandaccept_allflags - Fix typos: a
did_you_meansuggestion for mistakes likegmial.com - Know the company: a free
domain_insightobject with the organization’s name, industry, employee count and country
Call it from your server so your API token stays private:
async function verifyEmail(email) {
const params = new URLSearchParams({ email, token: process.env.FUEGO_KEY });
const res = await fetch(`https://app.gofuego.io/api/v1/verify?${params}`);
return res.json();
}
const check = await verifyEmail("[email protected]");
console.log(check.result); // "deliverable"
console.log(check.domain_insight.name); // "Jupiter Mining Corporation"
For a full walkthrough, see our guide to integrating the Fuego API with JavaScript.
In summary, a simple regex pattern is often “good enough” for client-side validation, ensuring that the basic structure is correct. For more stringent checks, pair it with an email verification API so you know the address is real.
Start a free trial to try it out.