Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL SQLITE MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST DART PLAYWRIGHT TOOLS MATH SHEETS POWER BI

HTML Tutorial

HTML HOME HTML Introduction HTML Editors HTML Basic HTML Elements HTML Attributes HTML Headings HTML Paragraphs HTML Styles HTML Project I HTML Formatting HTML Quotations HTML Comments HTML Colors HTML CSS HTML Project II HTML Links HTML Images HTML Favicon HTML Page Title HTML Tables HTML Lists HTML Project III HTML Block & Inline HTML Div HTML Classes HTML Id HTML Project IV HTML Global Attributes HTML Buttons HTML Iframes HTML JavaScript HTML File Paths HTML Head HTML Layout HTML Responsive HTML Project V HTML Computercode HTML Semantics HTML Project VI HTML Dialog HTML Details HTML Style Guide HTML Entities HTML Symbols HTML Emojis HTML Character Sets HTML Languages HTML URL Encoding HTML Performance HTML SEO

HTML Forms

HTML Forms HTML Form Attributes HTML Form Elements HTML Input Types HTML Input Attributes Input Form Attributes HTML Form Validation HTML Progress and Meter HTML Project VII

HTML Graphics

HTML Canvas HTML SVG

HTML Media

HTML Media HTML Video HTML Audio HTML YouTube HTML Project VIII

Web Accessibility

Web Accessibility ARIA Attributes Keyboard Focus and tabindex Landmarks Skip Links Color Contrast

Web APIs

Web APIs Geolocation API Drag and Drop API Popover API Web Storage API Web Workers API Server-Sent Events API

HTML Cert

HTML Certificate

HTML Examples

HTML Examples HTML Videos HTML Editor HTML Quiz HTML Exercises HTML Challenges HTML Projects HTML Website HTML Syllabus HTML Study Plan HTML Interview Prep HTML Summary

HTML References

HTML Tag List HTML Attributes HTML Global Attributes HTML Browser Support HTML Event Attributes HTML Colors HTML Canvas HTML Audio/Video HTML Doctypes HTML Character Sets HTML URL Encode HTML Language Codes HTML Country Codes HTTP Status Codes HTTP Request Methods PX to EM Converter Keyboard Shortcuts

HTML Form Validation


Share

What is Form Validation?

Form validation means checking that the user filled in a form correctly.

For example, a name field must not be empty, and an email address must contain an @ sign.

HTML can do these checks for you.

You add attributes to the input fields, and the browser checks the values when the user clicks Submit.

If a value is wrong, the browser stops the form, marks the field, and shows a short message.

You do not need any JavaScript.


Required Fields

The required attribute means that the field must be filled in:

Example

<form action="/action_page.php">
  <label for="fname">First name:</label>
  <input type="text" id="fname" name="fname" required>
  <input type="submit" value="Submit">
</form>
Try it Yourself »

If you click Submit with an empty field, the browser displays a message like this:

Please fill out this field.

Note: Each browser writes its own messages. The text can be different in your browser and in your language.



Validation by Input Type

Some input types check the value by themselves.

A type="email" field only accepts text that looks like an email address:

Example

<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
Try it Yourself »

If you type "hello" and click Submit, the browser displays a message like this:

Please include an '@' in the email address. 'hello' is missing an '@'.

The type="url" and type="number" also check the value by themselves.

Tip: Learn about all input types in the HTML Input Types chapter.


Numbers: min and max

The min and max attributes set the smallest and the largest number the user can enter:

Example

<label for="age">Age (18 to 99):</label>
<input type="number" id="age" name="age" min="18" max="99" required>
Try it Yourself »

If you type 12 and click Submit, the browser displays a message like this:

Value must be greater than or equal to 18.

Tip: The min and max attributes also work with dates and times.


Text Length: minlength and maxlength

The minlength and maxlength attributes set how many characters the text can have:

Example

<label for="username">Username (4 to 12 characters):</label>
<input type="text" id="username" name="username" minlength="4" maxlength="12" required>
Try it Yourself »

If you type "abc" and click Submit, the browser displays a message like this:

Please lengthen this text to 4 characters or more (you are currently using 3 characters).

The maxlength attribute works in another way.

It does not show a message, it simply stops the user from typing more than 12 characters.


Patterns

The pattern attribute checks the value against a regular expression (a short code that describes what the text must look like).

Use the title attribute to tell the user what you expect:

Example

A product code must be three capital letters followed by three digits:

<label for="code">Product code:</label>
<input type="text" id="code" name="code"
pattern="[A-Z]{3}[0-9]{3}"
title="Three capital letters and three digits, like ABC123" required>
Try it Yourself »

If you type "abc12" and click Submit, the browser displays a message like this, often with the text from title under it:

Please match the requested format.
Three capital letters and three digits, like ABC123

Tip: Learn more about regular expressions in our JavaScript tutorial.


Validation Attributes

Attribute What it checks
required The field is not empty
type The value fits the type, like email, url, or number
min and max The number or date is not too small or too large
minlength and maxlength The text is not too short or too long
pattern The text matches a regular expression
step The number fits the step, like whole numbers only

Style Valid and Invalid Fields

CSS can show the user which fields are wrong.

The CSS :user-invalid pseudo-class styles a field with a wrong value, and :user-valid styles a field with a correct value:

Example

input:user-invalid {
  border: 2px solid red;
}

input:user-valid {
  border: 2px solid green;
}
Try it Yourself »

Tip: These styles only apply after the user has changed the field, or tried to submit the form.

Tip: Do not use color alone to show an error. Some users cannot see the difference between red and green, so also use a clear border, an icon, or a text message.


Turn Off Validation

The novalidate attribute on the <form> element turns off all browser checks:

Example

<form action="/action_page.php" novalidate>

This is useful when you want to write your own checks with JavaScript.


Always Check on the Server Too

Browser validation helps users fix mistakes quickly. However: It does not protect your website!

A user can turn off the checks, change the HTML, or send data to the server without using your form at all.

The server must always check the data again before it uses it.


Summary

  • The browser checks form fields when the user clicks Submit
  • required makes a field mandatory
  • Input types like email and number check the format
  • min, max, minlength, maxlength, and pattern set the rules
  • :user-invalid and :user-valid style the fields
  • Always check the data on the server too

Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by W3Schools. All Rights Reserved. W3Schools is Powered by W3.CSS.