We're looking for a user researcher to understand the needs of developers and designers. Is this you or someone you know? Check out the post: https://mzl.la/2IGzdXS


创建web表单始终是一个复杂的任务. 仅仅组装表单是容易的,但是检查每一个字段的值是否有效并且一致是一件更加困难的事情,而向用户指明错误可能会令人头痛。HTML5 引入了表单相关的一些新的机制:为<input>元素和强制校验增加了一些新的语义类型,使得在客户端检查表单内容的工作变得容易。基本上,在填写字段时,通常这些约束都会被检查,而不需要额外的JavaScript代码进行校验; 对于更复杂的约束条件的校验可以尝试使用 HTML5 Constraint Validation API.

Note: HTML5 Constraint validation doesn't remove the need for validation on the server side. Even though far fewer invalid form requests are to be expected, invalid ones can still be sent by non-compliant browsers (for instance, browsers without HTML5 and without JavaScript) or by bad guys trying to trick your web application. Therefore, like with HTML4, you need to also validate input constraints on the server side, in a way that is consistent with what is done on the client side.


在 HTML5中,声明基本的约束有两种方式:

  • <input> 元素的  type 特性选择最合适的语义化的值,比如,选择 email 类型将会自动创建一个约束用于检查输入的值是否是一个有效的 e-mail 地址。
  • 设置验证相关的特性值,允许用一种简单的方式来描述基本的约束,而不必要使用 JavaScript。

语义的 input 类型

 type 特性中固有约束:

Input 类型 约束描述 Associated violation
<input type="URL"> 值必须是绝对的URL, 即,是下面的某一种:
  • a valid URI (as defined in RFC 3986)
  • a valid IRI, without a query component (as defined in RFC 3987)
  • a valid IRI, with a query component without any unescaped non-ASCII character (as defined in RFC 3987)
  • a valid IRI, and the character set for the document is UTF-8 or UTF-16 (as defined in RFC 3987)
Type mismatch constraint violation
 <input type="email"> The value must follow the ABNF production: 1*( atext / "." ) "@" ldh-str 1*( "." ldh-str ) where:
  • atext is defined in RFC 5322, i.e., a US-ASCII letter (A to Z and a-z), a digit (0 to 9) or one of the following! # $ % & ' * + - / = ? ` { } | ~ special character,
  • ldh-str is defined in RFC 1034, i.e., US-ASCII letters, mixed with digits and - grouped in words separated by a dot (.).
Note: if the multiple attribute is set, several e-mail addresses can be set, as a comma-separated list, for this input. If any of these do not satisfy the condition described here, the Type mismatch constraint violation is triggered.
Type mismatch constraint violation

Note that most input types don't have intrinsic constraints, as some are simply barred from constraint validation or have a sanitization algorithm transforming incorrect values to a correct default. 



特性 支持该特性的 Input 类型 可接受的值 约束描述 Associated violation
pattern text, search, url, tel, email, password A JavaScript regular expression (compiled with the ECMAScript 5 global, ignoreCase, and multiline flags disabled) 输入的值必须匹配设置的模式。 Pattern mismatch constraint violation
min range, number A valid number 输入的值必须大于等于设置的最小值。 Underflow constraint violation
date, month, week A valid date
datetime, datetime-local, time A valid date and time
max range, number A valid number 输入的值必须小于等于设置的最大值。 Overflow constraint violation
date, month, week A valid date
datetime, datetime-local, time A valid date and time
required text, search, url, tel, email, password, date, datetime, datetime-local, month, week, time, number, checkbox, radio, file; also on the <select> and <textarea> elements none as it is a Boolean attribute: its presence means true, its absence means false There must be a value (if set). Missing constraint violation
step date An integer number of days Unless the step is set to the any literal, the value must be min + an integral multiple of the step. Step mismatch constraint violation
month An integer number of months
week An integer number of weeks
datetime, datetime-local, time An integer number of seconds
range, number An integer
maxlength text, search, url, tel, email, password; also on the <textarea> element An integer length The number of characters (code points) must not exceed the value of the attribute. Too long constraint violation

Constraint validation process

Constraint validation is done through the Constraint Validation API either on a single form element or at the form level, on the <form> element itself. The constraint validation is done in the following ways:

  • By a call to the checkValidity() method of a form-related DOM interface (HTMLInputElement, HTMLSelectElement, HTMLButtonElement or HTMLTextAreaElement), which evaluates the constraints only on this element, allowing a script to get this information. (This is typically done by the user-agent when determining which of the CSS pseudo-classes, :valid or :invalid, applies.)
  • By a call to the checkValidity() function on the HTMLFormElement interface, which is called statically validating the constraints.
  • By submitting the form itself, which is called interactively validating the constraints.
  • If the novalidate attribute is set on the <form> element, interactive validation of the constraints doesn't happen.
  • Calling the send() method on the HTMLFormElement interface doesn't trigger a constraint validation. In other words, this method sends the form data to the server even if doesn't satisfy the constraints.

Complex constraints using HTML5 Constraint API

Using JavaScript and the Constraint API, it is possible to implement more complex constraints, for example, constraints combining several fields, or constraints involving complex calculations.

Basically, the idea is to trigger JavaScript on some form field event (like onchange) to calculate whether the constraint is violated, and then to use the method field.setCustomValidity() to set the result of the validation: an empty string means the constraint is satisfied, and any other string means there is an error and this string is the error message to display to the user.

Constraint combining several fields: Postal code validation

The postal code format varies from one country to another. Not only do most countries allow an optional prefix with the country code (like D- in Germany, F- in France or Switzerland), but some countries have postal codes with only a fixed number of digits; others, like the UK, have more complex structures, allowing letters at some specific positions.

Note: This is not a comprehensive postal code validation library, but rather a demonstration of the key concepts. 

As an example, we will add a script checking the constraint validation for this simple form:

    <label for="ZIP">ZIP : </label>
    <input type="text" id="ZIP"> 
    <label for="Country">Country : </label>
    <select id="Country">
      <option value="ch">Switzerland</option>
      <option value="fr">France</option>
      <option value="de">Germany</option>
      <option value="nl">The Netherlands</option>
    <input type="submit" value="Validate">

This displays the following form: 

First, we write a function checking the constraint itself:

function checkZIP() {
  // For each country, defines the pattern that the ZIP has to follow
  var constraints = {
    ch : [ '^(CH-)?\\d{4}$', "Switzerland ZIPs must have exactly 4 digits: e.g. CH-1950 or 1950" ],
    fr : [ '^(F-)?\\d{5}$' , "France ZIPs must have exactly 5 digits: e.g. F-75012 or 75012" ],
    de : [ '^(D-)?\\d{5}$' , "Germany ZIPs must have exactly 5 digits: e.g. D-12345 or 12345" ],
    nl : [ '^(NL-)?\\d{4}\\s*([A-RT-Z][A-Z]|S[BCE-RT-Z])$',
                    "Nederland ZIPs must have exactly 4 digits, followed by 2 letters except SA, SD and SS" ]
  // Read the country id
  var country = document.getElementById("Country").value;

  // Get the NPA field
  var ZIPField = document.getElementById("ZIP");

  // Build the constraint checker
  var constraint = new RegExp(constraints[country][0], "");

  // Check it!
  if (constraint.test(ZIPField.value)) {
    // The ZIP follows the constraint, we use the ConstraintAPI to tell it
  else {
    // The ZIP doesn't follow the constraint, we use the ConstraintAPI to
    // give a message about the format required for this country

Then we link it to the onchange event for the <select> and the oninput event for the <input>:

window.onload = function () {
    document.getElementById("Country").onchange = checkZIP;
    document.getElementById("ZIP").oninput = checkZIP;

You can see a live example of the postal code validation.  

Limiting the size of a file before its upload

Another common constraint is to limit the size of a file to be uploaded. Checking this on the client side before the file is transmitted to the server requires combining the Constraint API, and especially the field.setCustomValidity() method, with another JavaScript API, here the HTML5 File API.

Here is the HTML part:

<label for="FS">Select a file smaller than 75 kB : </label>
<input type="file" id="FS">

This displays:


The JavaScript reads the file selected, uses the File.size() method to get its size, compares it to the (hard coded) limit, and calls the Constraint API to inform the browser if there is a violation:

function checkFileSize() {
  var FS = document.getElementById("FS");
  var files = FS.files;

  // If there is (at least) one file selected
  if (files.length > 0) {
     if (files[0].size > 75 * 1024) { // Check the constraint
       FS.setCustomValidity("The selected file must not be larger than 75 kB");
  // No custom constraint violation


Finally we hook the method with the correct event:

window.onload = function () {
  document.getElementById("FS").onchange = checkFileSize;

You can see a live example of the File size constraint validation.

Visual styling of constraint validation

Apart from setting constraints, web developers want to control what messages are displayed to the users and how they are styled.

Controlling the look of elements

The look of elements can be controlled via CSS pseudo-classes.

:required and :optional CSS pseudo-classes

The :required and :optional pseudo-classes allow writing selectors that match form elements that have the required attribute, or that don't have it.

:-moz-placeholder CSS pseudo-class

See :-moz-placeholder.

:valid :invalid CSS pseudo-classes

The :valid and :invalid pseudo-classes are used to represent <input> elements whose content validates and fails to validate respectively according to the input's type setting. These classes allow the user to style valid or invalid form elements to make it easier to identify elements that are either formatted correctly or incorrectly.

Default styles

Controlling the text of constraints violation

The x-moz-errormessage attribute

The x-moz-errormessage attribute is a Mozilla extension that allows you to specify the error message to display when a field does not successfully validate.

Note: This extension is non-standard.

Constraint API's element.setCustomValidity()

The element.setCustomValidity(error) method is used to set a custom error message to be displayed when a form is submitted. The method works by taking a string parameter error. If error is a non-empty string, the method assumes validation was unsuccessful and displays error as an error message. If error is an empty string, the element is considered validated and resets the error message.

Constraint API's ValidityState object

The DOM ValidityState interface represents the validity states that an element can be in, with respect to constraint validation. Together, they help explain why an element's value fails to validate, if it's not valid.

Examples of personalized styling

HTML4 fallback

Trivial fallback

JS fallback



此页面的贡献者: shiyi25928988, yongchen
最后编辑者: shiyi25928988,