Skip to main content
Validação HTML

Bad value X for attribute “required” on element “input”.

Sobre este problema HTML

In HTML, boolean attributes like required work differently from what many developers expect. Their presence on an element means the value is true, and their absence means the value is false. According to the WHATWG HTML specification, a boolean attribute’s value must either be the empty string or an ASCII case-insensitive match for the attribute’s canonical name. For the required attribute, this means the only valid values are "" (empty string) and "required".

A common mistake is writing required="true" or required="false". The value "true" is not a valid boolean attribute value in HTML and will trigger this validation error. Even more confusingly, writing required="false" does not make the input optional — since the attribute is still present, the browser still treats the field as required. This can lead to subtle bugs where a form field appears to be optional in your code but is actually enforced as required by the browser.

This matters for several reasons:

  • Standards compliance: Invalid attribute values violate the HTML specification and will cause W3C validation errors.
  • Code clarity: Using non-standard values like "true" or "false" misleads other developers about how the attribute works.
  • Unexpected behavior: required="false" still makes the field required, which can cause confusing form behavior.

To make a field optional, simply remove the required attribute entirely rather than setting it to "false".

Examples

Incorrect: Invalid values for required

These will all trigger the “Bad value for attribute required“ validation error:

<input type="text" required="true">
<input type="email" required="false">
<input type="text" required="yes">
<input type="text" required="1">

Correct: Valid uses of the required attribute

All three of these forms are valid and make the input required:

<!-- Preferred: no value (most concise) -->

<input type="text" required>

<!-- Also valid: empty string -->

<input type="text" required="">

<!-- Also valid: canonical name as value -->

<input type="text" required="required">

Correct: Making a field optional

To make a field optional, remove the attribute entirely:

<input type="text">

Full form example

<form action="/submit" method="post">
  <label for="name">Name (required):</label>
  <input type="text" id="name" name="name" required>

  <label for="notes">Notes (optional):</label>
  <input type="text" id="notes" name="notes">

  <button type="submit">Submit</button>
</form>

This same rule applies to other boolean attributes in HTML, such as disabled, checked, readonly, multiple, and autofocus. They all follow the same pattern: use the attribute with no value, with an empty string, or with the attribute’s own name as the value. Never assign "true" or "false" to them.

Encontre problemas como este automaticamente

O Rocket Validator analisa milhares de páginas em segundos, detetando problemas HTML em todo o seu site.

Ajude-nos a melhorar os nossos guias

Este guia foi útil?

Pronto para validar os seus sites?
Comece o seu teste gratuito hoje.