69 of 133 menu

The required attribute

The required attribute indicates that the given HTML form element must be filled in.

The attribute should be applied to the input or textarea tags.

When attempting to submit a form if the field with this attribute is not filled in, the browser will not allow the form to be submitted and will display an error in the form of a pop-up hint. Unfortunately, the error text and its appearance cannot be changed using HTML or CSS.

Please note that the presence of the required attribute does not exempt you from checking the correctness of the form filling on the server side in PHP (since the protection via the attribute is easy to bypass).

Example

Let's add the attribute required to the tag input. Without entering anything in the field, try clicking the button to submit the form. The browser will not allow you to submit the form, will add a shadow to the input and display an error message:

<form action=""> <input type="text" placeholder="enter something" required> <input type="submit"> </form>

:

See also

  • attribute pattern,
    which allows you to perform input validation
byenru