New to Rust? Grab our free Rust for Beginners eBook Get it free →
How to Validate Email Address in JavaScript

Checking an email address in JavaScript can help a form reject values that do not match its accepted format. An email input checks common syntax in the browser, while JavaScript can apply a narrower rule.
This article shows how to add an email field and connect a JavaScript check to form submission.
TL;DR
A signup form needs a quick way to flag mistyped addresses. Validate submitted data again on your server because a regular expression only checks a chosen text format.
- An email input provides built-in syntax checks.
- A JavaScript expression can enforce a narrower product rule.
- Only a confirmation message can show that a person can access the mailbox.
What Is Email Address Validation?
The local part appears before the at sign, and the domain appears after it. A format validator compares that text with a rule chosen by an application.
How to Validate an Email Address Step by Step
I ran node email_validation_demo.js to compare the custom function with the regex MDN documents as equivalent to an email input’s syntax check, using Node rather than a browser form.
Step 1: Add an email field
The field label names the control, and the required attribute asks the browser not to accept an empty value. Script-driven feedback can appear in the live region without moving the visitor away from the form.
<form id="signup">
<label for="email">Email address</label>
<input id="email" name="email" type="email" required>
<button>Continue</button>
</form>
<p id="message" aria-live="polite"></p>
The label gives the field an accessible name, and the live region can announce feedback when JavaScript updates it. Normal form submission also triggers the browser’s own validation message when a required value is missing or malformed. Browser messages vary, so add clear nearby guidance when the accepted format is narrower than the native rule.
Keep a text message with any color cue, so color is not the only clue for someone who needs to correct the value. Test the form with a keyboard to check the order of focus and feedback.
Step 2: Add a focused JavaScript check
Use this expression only when the form needs a defined rule beyond the browser’s built-in check.
function validateEmail(email) {
const pattern = /^(?!.*\.\.)[a-z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-z0-9](?:[a-z0-9-]*[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)+$/i;
return pattern.test(email);
}
The character class before the at sign lists letters, digits, and common punctuation accepted by this example. The domain expression requires labels that begin and end with letters or digits, with hyphens allowed inside each label. The anchors reject extra text before or after the address, but quoted local parts and international characters are outside this rule.
MDN documents this input-equivalent regular expression. Running it in Node lets us compare its text-matching behavior with the custom function without implying that a browser form was exercised.
const htmlEmailEquivalent = /^[\w.!#$%&'*+/=?^`{|}~-]+@[a-z\d](?:[a-z\d-]{0,61}[a-z\d])?(?:\.[a-z\d](?:[a-z\d-]{0,61}[a-z\d])?)*$/i;
“[email protected]” -> custom true, MDN-equivalent true
“[email protected]” -> custom true, MDN-equivalent true
“[email protected]” -> custom false, MDN-equivalent true
“dev@example” -> custom false, MDN-equivalent true
” [email protected] ” -> custom false, MDN-equivalent false

I saw the two expressions disagree on repeated dots and on an address without a dotted domain. The MDN-equivalent regex accepted both examples, while the custom rule rejected them.
Step 3: Connect validation to the form
When submission reaches this handler, check the value against the application’s acceptance rule. The code cancels the event when that check fails, returns focus to the field, and adds a message the live region can announce.
const form = document.querySelector("#signup");
const emailField = document.querySelector("#email");
const message = document.querySelector("#message");
form.addEventListener("submit", (event) => {
if (!validateEmail(emailField.value)) {
event.preventDefault();
message.textContent = "Enter an email address in the accepted format.";
emailField.focus();
}
});
A separate server check is necessary because a request can reach the endpoint without running browser code. Use the linked CodeForGeek Node.js validation example as a reference for validating incoming fields.
Email Validation Limits and Edge Cases
Several different decisions can happen between typing an address and activating an account.
| Case | What the check tells you | Useful next step |
|---|---|---|
| Empty value | Email syntax may allow it | Set required when needed |
| Address format | Input looks acceptable under a rule | Validate again on the server |
| Address with plus sign | Many systems permit tagged addresses | Do not reject it without a product reason |
| Mailbox ownership | Syntax cannot establish access | Send a one-time confirmation link |
I chose not to trim input silently because the leading-space sample fails. A list field can use HTML’s multiple attribute to accept comma-separated addresses, which the browser checks entry by entry. Keep that behavior for list entry rather than a single signup address.
The validator.js library exposes configurable isEmail options, including whether to require a top-level domain and allow a UTF-8 local part. Review those options against the addresses your service expects before relying on the library.
To verify account control, issue a short-lived confirmation link and record success when the recipient opens it. The CodeForGeek email verification tutorial shows that next step.
Conclusion
Choose error wording that tells a person how to correct the field. A terse message such as “invalid email” gives no clue whether the problem is a missing at sign, a space, or a form the application does not accept.
For browser behavior, consult MDN’s email input reference. For backend checks, see the linked CodeForGeek validation and email confirmation examples.
FAQ
These answers distinguish an address that looks acceptable from one that can receive a message.
Can JavaScript confirm that an email address exists?
No. JavaScript can check a selected format, but a confirmation message is needed to show that the person can access the mailbox.
Should I use a regular expression or an email input?
Use type=email for common browser syntax checks. Add a custom expression only when your form needs a specific additional rule.
Do I still need server-side validation?
Yes. Requests can bypass browser checks, so validate incoming values on the server before using them.




