VALIDATION11 min20 XP

Form Validation

Learn Form Validation with syntax-highlighted examples, live output, and Shiv AI.

HTML Form Validation

HTML5 added built-in form validation attributes that check user input before sending to the server — no JavaScript needed. The browser handles error messages automatically in the user's language.

Required Fields

Example — required Attribute
<form>
  <label for="email">Email *</label>
  <input
    type="email"
    id="email"
    required
    placeholder="you@example.com"/>
  <button type="submit">Submit</button>
</form>
Try clicking Submit with an empty or invalid email. The browser shows a validation error automatically.

Min, Max, and Pattern

Example — Validation Constraints
<!-- Number between 1 and 100 -->
<input type="number" min="1" max="100" required/>

<!-- Text must match pattern (Indian mobile) -->
<input type="text"
       pattern="[6-9][0-9]{9}"
       title="10-digit Indian mobile number"
       required/>

<!-- Password: min 8 characters -->
<input type="password"
       minlength="8"
       maxlength="64"
       required/>
pattern uses a regex to validate format. title shows as the error message hint when the pattern fails.

Visual Feedback with CSS

Example — :valid and :invalid Pseudo-classes
<style>
  /* Green border when valid */
  input:valid   { border-color: green; }
  /* Red border when invalid and user has typed */
  input:invalid:not(:placeholder-shown) {
    border-color: red;
  }
</style>
:not(:placeholder-shown) prevents showing red border on empty fields that the user has not touched yet.
Validation AttributesReference
requiredField must be filled before form submits
min maxMinimum and maximum value for number and date inputs
minlength maxlengthMinimum and maximum character count for text inputs
patternRegex pattern the value must match. Use title for the error hint
type="email"Built-in email format validation. Checks for @ and domain
type="url"Built-in URL validation. Must start with http:// or https://
novalidateOn form tag — disables built-in validation for custom JS validation
Client vs Server Validation

HTML validation runs in the browser but can be bypassed by anyone. Always also validate on the server. HTML validation is for user experience — quick feedback without a page reload. Server validation is for security.

Before HTML5 Validation

Before 2014 every form needed JavaScript validation manually coded. Developers wrote hundreds of lines to check emails, numbers, and required fields. HTML5 built-in validation replaced most of that with simple attributes.

CIWeb HTML Course • 22 Lessons • Free

Explain with Analogies

Shiv AI explains this lesson using only real-life analogies. No code at all.

Click the Explain tab to generate analogies automatically.
index.html
Live Preview
Start coding - Shiv AI monitors automatically

Which attribute prevents form submission until a field is filled?

Explanationrequired prevents the form from submitting if the field is empty. The browser shows an automatic error message in the user's own language.
Build Challenge
Validated Registration Form
Build a registration form with required, minlength, min/max, and CSS :valid/:invalid styling.
Requirements (6 to pass)
form
required
minlength or min
valid css
invalid css
two inputs
solution.html
Preview
Community Wall
Lesson 19
0/500
0/20 lines
as
Recent Posts
Lesson 19 only

Support CIWeb + Get +50 AI Messages

Just Rs.11 gives you 50 extra Shiv AI messages and keeps CIWeb free for everyone.

AI
Shiv AI
Online