Teyro
CoursesFor creatorsBlog
Log inGet started
Tey Mascot
LOADING.

Every expert you admire was once a beginner who refused to quit.

Teyro

The fun way to finish learning coding and AI: short daily lessons with streaks, leagues and friends.

Get the app

Learn

  • All courses
  • Learn coding
  • Learn AI
  • Learn online
  • How it works
  • Leagues
  • Get the app

Creators

  • Teach on Teyro
  • How teaching works
  • Teach coding online
  • Teach AI online
  • Join as a Founding Creator
  • Creator log in

Resources

  • Blog
  • Features
  • Use cases
  • Compare
  • FAQ

Legal

  • Terms of Use
  • Privacy Policy

© 2026 Teyro · support@teyro.app

HomeBlogSkill Building & Careers
Skill Building & Careers

JavaScript Form Validation Example (With Error Messages)

A simple JavaScript form validation example with HTML, error messages and checks for empty fields and email format, plus tips on why server checks still matter.

The Teyro TeamOct 25, 20262 min read
On this page9 sections
  1. Direct answer
  2. HTML
  3. JavaScript
  4. Good practice
  5. HTML-only checks
  6. Related
  7. Where Teyro fits
  8. The bottom line
  9. More coding guides

Forms are where users make mistakes. Good validation tells them what to fix, kindly and clearly.

Direct answer

To validate a form with JavaScript, listen for the form's submit event, check each field, show an error message next to any invalid field, and call event.preventDefault() to stop the form being sent until everything is valid. Always repeat the checks on the server too.

HTML

<form id="signup" novalidate>
  <label>
    Email
    <input type="email" id="email" name="email">
  </label>
  <p id="email-error" role="alert"></p>

  <label>
    Password
    <input type="password" id="password" name="password">
  </label>
  <p id="password-error" role="alert"></p>

  <button type="submit">Sign up</button>
</form>

JavaScript

const form = document.querySelector('#signup');
const email = document.querySelector('#email');
const password = document.querySelector('#password');
const emailError = document.querySelector('#email-error');
const passwordError = document.querySelector('#password-error');

form.addEventListener('submit', (event) => {
  let valid = true;

  if (email.value.trim() === '') {
    emailError.textContent = 'Please enter your email.';
    valid = false;
  } else if (!email.value.includes('@')) {
    emailError.textContent = 'That does not look like an email address.';
    valid = false;
  } else {
    emailError.textContent = '';
  }

  if (password.value.length < 8) {
    passwordError.textContent = 'Use at least 8 characters.';
    valid = false;
  } else {
    passwordError.textContent = '';
  }

  if (!valid) {
    event.preventDefault();
  }
});

Good practice

  • Write messages that say how to fix the problem.
  • Put the message next to the field.
  • Use role="alert" so screen readers announce errors.
  • Don't blame the user.
  • Validate on the server as well; client checks can be bypassed.

HTML-only checks

Attributes like required, type="email" and minlength="8" give simple validation without JavaScript.

Related

JavaScript DOM tutorial and JavaScript for beginners.

Where Teyro fits

Teyro's Interactive Web Pages with JavaScript course teaches you to check forms, react to taps and typing and build lists from data, with a live page in every lesson. It is free to start.

The bottom line

Help the user fix it, and check again on the server.

More coding guides

  • JavaScript DOM tutorial
  • JavaScript for beginners
  • How to make a website for beginners
  • Interactive JavaScript course

Frequently asked questions

How do I validate a form with JavaScript?

Listen for the submit event, check each field, show a message next to any problem and call preventDefault to stop sending until it is fixed.

Is client-side validation enough?

No. It helps users, but anyone can bypass it, so always validate on the server too.

Can HTML validate forms without JavaScript?

Yes, attributes like required, type=email and minlength give basic checks. JavaScript adds custom messages and rules.

How do I show an error message?

Put an empty element next to the field and set its textContent when the value is invalid.

Build forms that help people

Interactive Web Pages with JavaScript includes checking forms on a live page. 37 short lessons, free to start.

See the Interactive JavaScript course

Free to start · No credit card needed

Share

Written by

The Teyro Team

Learning Team at Teyro

We build Teyro — an AI-powered learning platform that turns skill-building into a daily habit through bite-sized, gamified lessons. We write about the learning science and study strategies that shape our product.

More from The Teyro Team

On this page

  • Direct answer
  • HTML
  • JavaScript
  • Good practice
  • HTML-only checks
  • Related
  • Where Teyro fits
  • The bottom line
  • More coding guides

Keep reading

Skill Building & Careers

Best Careers for 2027: High-Demand Jobs and the Skills to Get Them

The best careers for 2027 and the jobs most in demand, based on BLS and World Economic Forum data, plus the skills each path needs.

Oct 30, 20263 min read
Skill Building & Careers

Best Certifications for 2027: Tech, Data, Cloud and Business

The best certifications for 2027 in cloud, security, data, project management and AI, who each one suits, and when a portfolio matters more.

Oct 30, 20263 min read
Skill Building & Careers

Best Programming Language to Learn in 2027 (for Your Goal)

The best programming language to learn in 2027 depends on what you want to build. Compare Python, JavaScript, TypeScript, SQL and C.

Oct 30, 20263 min read