100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
HTML & CSS

HTML Form Validation

Learn how HTML5 provides built-in client-side form validation using attributes like required, pattern, and type.

Forms & ValidationBeginner10 min readJul 8, 2026
Analogies

Introduction

Before HTML5, validating form input required JavaScript for even simple checks like 'is this field empty?' or 'is this a valid email?'. HTML5 introduced native form validation, allowing browsers to check input values automatically using attributes on <input>, <select>, and <textarea> elements. This reduces the amount of JavaScript needed and gives users instant, accessible feedback without a round trip to the server.

🏏

Cricket analogy: Before HTML5's native validation, checking a form field was like needing a third umpire review for every routine decision; native validation is like the on-field umpire making instant calls without escalating every simple case.

Syntax

html
<form>
  <label for="email">Email:</label>
  <input type="email" id="email" name="email" required>

  <label for="age">Age:</label>
  <input type="number" id="age" name="age" min="18" max="99" required>

  <label for="username">Username (letters/numbers only):</label>
  <input type="text" id="username" name="username" pattern="[A-Za-z0-9]{3,16}" required>

  <button type="submit">Submit</button>
</form>

Explanation

The 'required' attribute prevents form submission until the field has a value. Input types such as email, url, number, and tel trigger automatic format checking (for example, 'email' requires an @ symbol and a domain). The 'pattern' attribute accepts a regular expression that the value must match. Numeric inputs use 'min' and 'max' to constrain the range, and text-based inputs can use 'minlength' and 'maxlength' to constrain length. When a field fails validation, the browser blocks submission and displays a default error message, and the ':invalid' and ':valid' CSS pseudo-classes let you style fields based on their validation state.

🏏

Cricket analogy: The required attribute is like a mandatory toss coin flip that must happen before play starts, the pattern attribute is like a specific bowling action rule the delivery must match, and the :invalid pseudo-class is like a no-ball signal styling the delivery differently the moment it breaks a rule.

You can use the Constraint Validation API in JavaScript (e.g., input.checkValidity(), input.setCustomValidity('message')) to customize error messages or add validation logic beyond what HTML attributes provide.

Native HTML validation only runs on the client. Always re-validate and sanitize data on the server, since client-side checks can be bypassed by disabling JavaScript or sending requests directly.

Example

html
<form novalidate id="signupForm">
  <label for="pwd">Password:</label>
  <input type="password" id="pwd" name="pwd" minlength="8" required>
  <span class="error" aria-live="polite"></span>

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

<script>
  const form = document.getElementById('signupForm');
  const pwd = document.getElementById('pwd');
  form.addEventListener('submit', (e) => {
    if (!pwd.checkValidity()) {
      e.preventDefault();
      pwd.nextElementSibling.textContent = 'Password must be at least 8 characters.';
    }
  });
</script>

Key Takeaways

  • The 'required' attribute makes a field mandatory before submission.
  • Input types (email, url, number, tel) enable automatic format validation.
  • The 'pattern' attribute uses a regular expression for custom format rules.
  • min, max, minlength, and maxlength constrain numeric and text ranges.
  • Client-side validation improves UX but must always be backed by server-side validation.

Practice what you learned

Was this page helpful?

Topics covered

#HTMLCSS#HTMLCSSStudyNotes#WebDevelopment#HTMLFormValidation#HTML#Form#Validation#Syntax#StudyNotes#SkillVeris