The required attribute is a form validation attribute that can be applied to any standard form field in which a value can be entered. Upon form submission, if the form contains any elements with required attributes where the field’s value is empty, validation will fail and the form submission will be prevented.

The required attribute, alongside other new HTML5 input types and attributes is a great helper in form validation syntax and helps us keeping custom made JavaScript validations (or similar) minimized.

Syntax:

<input type="text" name="query" required />

Elements:

NOTE: the validation triggered by the required attribute can be bypassed through the use of the novalidate or formnovalidate attributes.

Example

The required attribute example:

 

›› go to examples ››