Loading...

HTML essentialsContent

Form validation & types

6 min lesson

Next
  • Validation checks that the data a user types is correct before the form is sent.
  • HTML gives free browser checks through input types and attributes like required, min, max, minlength and pattern.
  • Think of validation as a bouncer at the door: it stops wrong data before it reaches the server.
  • The right type (email, number, url) already checks basic format without any JavaScript.
  • Browser validation is a first line of defense; the server must always validate again.
  • This builds on Form controls: the same inputs now carry rules.
  • The example shows an email field, a number field with a range and a required text field; you will see the browser block wrong input.
  • Next, Form submission: GET vs POST & file uploads explains how the checked data is sent.
Keep in mind
  • Prefer built-in types and attributes over custom scripts for simple checks.
  • Use required for mandatory fields and pattern for simple formats.
  • Always validate on the server too; users can bypass the browser.
  • Write helpful error text so users know how to fix the problem.
index.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Validation</title>
</head>
<body>
  <form action="/submit" method="post">
    <!-- type=email checks for an @ and a domain -->
    <label for="email">Email</label>
    <input type="email" id="email" name="email" required>
    <br>
    <!-- type=number with min and max limits the allowed range -->
    <label for="age">Age</label>
    <input type="number" id="age" name="age" min="1" max="120" required>
    <br>
    <!-- minlength sets the minimum number of characters -->
    <label for="user">Username</label>
    <input type="text" id="user" name="user" minlength="3" maxlength="12" required>
    <br>
    <button type="submit">Sign up</button>
  </form>
</body>
</html>

What happens: A form with "Email", "Age" and "Username" fields and a "Sign up" button. Typing an invalid email or an age outside 1-120 and pressing Sign up shows a browser message and blocks submission.

Breaking it down

type=email

<input type="email" required>

Checks for a basic email shape on submit. On a phone it also opens the email keyboard.

required

<input required>

Blocks submission until the field has a value. Works on most input types.

min and max

<input type="number" min="1" max="120">

Limits the numeric range. The browser shows a message if the value is outside.

minlength and maxlength

<input minlength="3" maxlength="12">

Sets the smallest and largest allowed number of characters in a text field.

pattern

<input pattern="[0-9]{6}">

A regular expression the value must match. Useful for PIN codes and postcodes.

Validation attributes

AttributeChecks
requiredField is not empty
type=emailBasic email shape
min / maxNumeric or date range
minlength / maxlengthText length
patternCustom regular expression

A helpful analogy

Validation is like a quality check at a factory gate. Items that do not meet the rules are stopped before shipping, so the warehouse (the server) only receives good items.

Nesting rules

  • 1.Use min and max only on types that accept numbers or dates.
  • 2.Keep patterns simple; complex ones are hard to read and easy to get wrong.

Common mistakes

Relying only on browser validation

Avoid

Server trusts whatever arrives

Use instead

Server checks every value again

A user can disable or bypass browser checks. Only server-side checks truly protect the data.

Using maxlength as a security limit

Avoid

maxlength="12" and nothing else

Use instead

maxlength plus a server check

maxlength only helps the user. A direct request can send longer values, so the server must enforce limits.

Quick tips

  • •Submit an invalid form on purpose to see the built-in error messages.
  • •Use type=tel instead of pattern for phone numbers; formats vary too much for a strict pattern.
Previous
Next