Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Semantic input error message inside label

I'm using <label> to wrap an input such that its success and error messages can appear inside its label for association. What do you think is the most semantic markup for this scenario?

<label>
  <b>Username</b>
  <input>
  <strong>Username already taken :(</strong>
</label>

For errors is strong appropriate? Or is span better? Is role=status appropriate?

like image 911
ryanve Avatar asked Oct 09 '17 22:10

ryanve


People also ask

Can you put an input inside a label?

There are two ways to pair a label and an input. One is by wrapping the input in a label (implicit), and the other is by adding a for attribute to the label and an id to the input (explicit). Think of an implicit label as hugging an input, and an explicit label as standing next to an input and holding its hand.

How do you show error messages in HTML?

We can show errors with two methods without using the alert box. Syntax: node. textContent = "Some error message" // To draw attention node.

What does label do in input?

The label is used to tell users the value that should be entered in the associated input field.

What is the HTML tag for error message?

There is no right tag to use for an error message. It all depends on how and where you want to display the error. Once you decide on these things, your choices will be narrowed, as tag properties and limitations differ.


2 Answers

The WCAG provides an example of error displaying using both aria-invalid and aria-describedby properties.

In your example, the code would be:

<label>
  <b>Username</b>
  <input aria-invalid="true" aria-describedby="error">
  <strong id="error">Username already taken :(</strong>
</label>

The strong is appropriate as it appears to be an important notice. An alert role (rather than status) would be more appropriate to be applied to the #error element according to the W3C.

like image 59
Adam Avatar answered Sep 27 '22 22:09

Adam


Using a strong tag is appropriate.

Strong tags signify something is of bigger importance, and the password being incorrect is important, as it blocks the user from proceeding.

like image 37
jeffkmeng Avatar answered Sep 27 '22 23:09

jeffkmeng