#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
DOM & Validation

JavaScript Email Address validation using Regular Expression

Why Validate Email with Regex?

Regular expressions let you check whether a user's input matches the general structure of a valid email address before submitting a form.

Example

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

console.log(isValidEmail("test@example.com")); // true
console.log(isValidEmail("invalid-email"));      // false

Key Points

  • Regex validation checks the format only — it doesn't confirm the email actually exists.
  • Always pair client-side validation with server-side validation for security.

How the Pattern Works, Piece by Piece

The pattern /^[^\s@]+@[^\s@]+\.[^\s@]+$/ looks cryptic, but it reads left to right in small chunks.

PartMeaning
^Start of the string, so nothing can come before the email
[^\s@]+One or more characters that are not a space and not an @ (the local part)
@Exactly one literal @ sign
[^\s@]+The domain name, again with no spaces or extra @
\.A literal dot (the backslash stops it meaning "any character")
[^\s@]+$The ending, such as com or in, up to the end of the string

Without ^ and $, a string like hello test@example.com world would still pass, because the pattern would match a piece in the middle.

Wiring It into a Real Form

Checking a value in the console is a start. In a real page you validate when the user submits, show a helpful message, and trim stray spaces first.

const form = document.querySelector("#signup");
const input = document.querySelector("#email");
const error = document.querySelector("#email-error");
const EMAIL_RE = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;

form.addEventListener("submit", (event) => {
  const value = input.value.trim();

  if (!EMAIL_RE.test(value)) {
    event.preventDefault();               // stop the submit
    error.textContent = "Please enter a valid email, like name@example.com";
    input.setAttribute("aria-invalid", "true");
    input.focus();
  } else {
    error.textContent = "";
    input.removeAttribute("aria-invalid");
  }
});

Browsers also offer built-in checking with <input type="email" required>. That gives you a free first layer, and you can read the result with input.checkValidity(). Many teams use the native check and add a custom regex only when they need stricter rules, such as requiring a dot in the domain.

Choosing Between Simple and Strict Patterns

A fully standards-compliant email regex is extremely long and still cannot guarantee delivery. A simple pattern catches typing mistakes, which is what users actually make. Overly strict patterns cause real harm: they reject valid addresses with plus signs, long domain endings, or subdomains.

  • Use a simple pattern for sign-up and contact forms.
  • Use a confirmation email when you must know the address really works.
  • Use a trusted validation library on the server if you need consistent rules across many apps.

Common Mistakes

  • Forgetting to trim(), so a trailing space fails validation.
  • Using the g flag with test(); the regex remembers lastIndex and alternates between true and false.
  • Rejecting uppercase letters or plus signs, which are valid.
  • Trusting client checks alone; anyone can bypass browser code, so the server must validate again.

Repeating the Check on the Server

Whatever language your back end uses, run the same simple format check there, normalise the address by trimming spaces and lowercasing the domain, and then send a verification link. Only mark the address as confirmed once the user clicks it. This keeps typos out of your database and stops anyone from signing up with an address they do not own.

Ready to master real-world JavaScript development?

Learn JavaScript hands-on with mentor-led, live sessions.

Explore Course