Validation and required inputs
Validation types
There are three main options for when validation can happen.
- onSubmit
- When the user moves on to the next step or submits the form.
- onBlur
- When the user moves the focus out of an input.
- onChange
- When the user changes the content or value of an input.
We mainly use onSubmit
because it is good enough for users and
doesn’t take too long to code. In special cases, for example when the correct
input requirements are higher, we can also use onBlur
.
Writing validation messages
- Make the message relevant to the triggered error.
- Tell users how they can fix their mistake and move along.
- Avoid using technical words, it’s the user who must fix the error.
- Give users essential information up front, don’t use the error message as a place to start explaining the interface and service requirements.
- See the writing guidelines for more on how to write for Mybring users.
Anatomy
Default
Place error messages inside the label tag, below the label text. This is to make sure they also work on a screen reader and won’t be covered by dropdown menus, or autofill. The message should have the role attribute set to alert.
<form class="mb-form maxw16r">
<label for="content">
Content *
<span class="error-help db" role="alert">Content is required</span>
</label>
<input class="error-input" id="content" type="text" />
</form>
Input groups
When there are many small inputs side by side in a group, messages can be listed underneath the row or group of inputs for a tidier layout. This usually requires a more precise message for the user to connect it to the corresponding input. Every message should have the role attribute set to alert.
<form class="mb-form maxw16r">
<fieldset>
<legend>
Size
</legend>
<div class="flex grm gcs mbm">
<label>
Height *
<input class="error-input" type="text" />
</label>
<label>
Width
<input type="text" />
</label>
<label>
Length *
<input class="error-input" type="text" />
</label>
</div>
<ul class="error-help">
<li role="alert">Height is required</li>
<li role="alert">Length is required</li>
</ul>
</fieldset>
</form>
Required and optional
Generally speaking, the mandatory inputs in Mybring should have the required attribute and can be marked with an asterisk in the same colour as the text. Optional inputs don’t need any special indication.
<form class="mb-form mb-form--spaced maxw16r">
<label for="req">Required text input *</label>
<input type="text" id="req" required />
<label for="opt">Optional text input</label>
<input type="text" id="opt" />
</form>