Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

Form Validation – Built‑in Constraints | HTML Course lesson | BraveProgrammer
  1. Courses
  2. /
  3. HTML Course

Lesson 8 of 55

·HTML

Form Validation – Built‑in Constraints

9 min

Course progress15%

What you'll learn

Keep these ideas in mind as you work through the lesson.

5 points
  • Constraint validation attributes (`required`, `pattern`, `min`, `max`, `step`)
  • Automatic error messages and the `:invalid` pseudo‑class
  • Custom validation with JavaScript (`setCustomValidity`, `checkValidity`)
  • When to disable native validation (`novalidate`)
  • Security: server‑side validation is mandatory

Browser‑Based Validation

HTML5 introduces constraint validation that runs automatically when a form is submitted.

<input type='email' name='email' required pattern='.+@example\.com' />

Common constraints

  • required – Field must not be empty.
  • pattern – Regular expression match. Use the title attribute to explain the required format to users.
  • min, max, step – Numeric range control.
  • type='number', type='url', type='date' – Built‑in format checks.
  • maxlength – Maximum number of characters.

Custom validation with JavaScript

const email = document.querySelector('input[name="email"]');
email.addEventListener('input', () => {
  if (!email.checkValidity()) {
    email.setCustomValidity('Please enter a corporate email ending with @example.com');
  } else {
    email.setCustomValidity('');
  }
});

setCustomValidity overrides the default message. When the string is empty, the control is considered valid.

Validation UI

  • Browsers display a native tooltip on invalid fields. You can style the :invalid pseudo‑class to highlight errors.
  • For a fully custom UI, add novalidate to the <form> and manage all checks in JavaScript. Use reportValidity() to trigger native messages if you need them.

Server‑side requirement

Never rely solely on client‑side validation; always re‑validate on the server to protect against tampering.


Quick checklist for validation

  1. All required fields have required? ✅
  2. Custom patterns use title for user guidance? ✅
  3. setCustomValidity used where native messages insufficient? ✅
  4. Server‑side validation planned for all inputs? ✅
  5. novalidate only used when you replace native checks? ✅

Tip: Use the Constraint Validation API (checkValidity(), reportValidity()) for a smooth hybrid approach.

Make it stickPractice what you've learned1 quiz · 2 practical tests · Optional activities from HTML CourseBrowse activities
Previous lessonButton Controls – Submit, Reset, ButtonContinue to lesson 9Accessibility – ARIA Roles and LabelsBack to HTML Course curriculum