Building accessible forms

Do not auto-focus inputs

  • Automatically moving focus to an input field is very confusing for people using assistive technology.

Group inputs

Use fieldset and legend (directly under the first fieldset tag) that describes groupings such as

  • Sign in
  • Shipping address
  • Payment information

Error handling

  • Individual inputs must have programmatically described errors read by the screen reader on focus
  • For long forms, it can be helpful to list all errors in an alert with links back to individual invalid inputs on each attempt to submit

UX guidance

Affordance: field width indicates the expected input

Form field width should afford the user space to enter the characters that will be required. Do not arbitrarily limit the width of names, usernames, passwords or emails.

Practical examples

  • Middle initial should be wide enough to accommodate 1 character
  • State abbreviations should be wide enough to accommodate 2 characters
  • Zip code must be wide enough to accommodate 5 characters
  • Pin numbers reflect the number of digits expected

Why we stack inputs

Do not put forms in multiple columns.

  • People are accustomed to scrolling vertically. There is no advantage to making the page take up less vertical space.
  • People with low vision may be using a zoom tool, enlarging the view of their screen and thus only seeing a portion of the form. If there is a column on the right side, it will be difficult to discover the fields.
  • Do not place submit buttons in a sidebar unless there is also a submit button at the bottom of the form

Why we use autocomplete

  • Autocomplete is helpful for all customers leading to a speedier conversion
  • For those with motor disabilities, it eliminates the need to laboriously enter information

Code examples

Use semantic HTML

Sign in
<form aria-label="Sign in">
  <fieldset>
    <legend>
      Sign in
    </legend>
    <label for="username">
      Username
    </label>
    <input type="text" 
            id="username">
    <button type="submit">
      Sign in
    </button>
  </fieldset>
</form>

Credit card information

  • This form uses minimal unobtrusive masking to make the credit card number more readable. (When done poorly, masking can can cause the field to be read repeatedly as the mask refreshes)
  • Autofill attributes to help customers complete fields with less effort.
  • Using inputmode="numeric" brings up the numeric keyboard on mobile devices making entry easier.
Enter your payment information
Secure form protected by 8 bit encryption
Expiration
<form aria-label="Payment information">
  <fieldset>
    <legend>
      Enter your payment information
    </legend>
    <label for="card-number">Card Number</label>
    <input type="text" 
            name="cardnumber" 
            id="card-number" 
            autocomplete="cc-number"
            inputmode="numeric"
            pattern="[0-9]*"
            required>
      <div id="card-number-hint" class="hint secure-icon">
        Secure form protected by 8 bit encryption
      </div>
    <fieldset>
      <legend>
        Expiration <span aria-hidden="true">(MM YY)</span>
      </legend>
      <div class="expiry-container">
        <label class="hidden" for="cc-exp-month">Expiration Month (MM)</label>
        <input type="text"
              maxlength="2"  
              name="ccmonth" 
              id="cc-exp-month"
              inputmode="numeric"
              autocomplete="cc-exp-month"
              pattern="[0-9]*"
              required>
        <label class="hidden" for="cc-exp-year">Expiration Year (YY)</span></label>
        <input type="text"
              maxlength="2"  
              name="ccyear" 
              id="cc-exp-year"
              inputmode="numeric"
              autocomplete="cc-exp-year"
              pattern="[0-9]*"
              required>
      </div>
    </fieldset>
    <label for="cc-cvc">Security code</label>
    <input type="text" 
            maxlength="5" 
            name="cvc" 
            id="cc-cvc"  
            autocomplete="cc-csc"
            inputmode="numeric"
            pattern="[0-9]*"
            required>
    <label for="cc-name">Name on card</label>
    <input type="text"
          name="ccname" 
          id="cc-name"
          placeholder="Full Name" 
          autocomplete="cc-name"
          maxlength="19" 
          required>
  </fieldset>
</form>

Shipping information

Shipping information
<form aria-label="Shipping information">
  <fieldset>
    <legend>
      Shipping information
    </legend>

    <label for="address-line1">
      Address line 1
    </label>
    <input type="text" 
            id="address-line1"
            autocomplete="address-line1">
      
    <label for="address-line2">
      Address line 2
    </label>
    <input type="text" 
            id="address-line2"
            autocomplete="address-line2">
    
    <label for="address-level2">
      City
    </label>
    <input type="text" 
            id="address-level2"
            autocomplete="address-level2">

    <label for="address-level1">
      State
    </label>
    <input type="text" 
            id="address-level1"
            autocomplete="address-level1"
            maxlength="2">

    <label for="postal-code">
      Zip postal code
    </label>
    <input type="text" 
            id="postal-code"
            inputmode="numeric"
            pattern="[0-9]*"
            autocomplete="postal-code"
            maxlength="5">
  </fieldset>
</form>

Contact form

Preferred contact method
Your information
We’ll never sell or share your information
Format: 573-268-9692
<form aria-label="Contact us">
  <fieldset>
    <legend>
      Preferred contact method
    </legend>
  
    <input type="radio" name="method" id="contact-email" checked>
    <label for="contact-email">Email</label>
  
    <input type="radio" name="method" id="contact-sms">
    <label for="contact-sms">SMS text</label>
  
    <input type="radio" name="method" id="contact-phone">
    <label for="contact-phone">Phone</label>
  </fieldset>

  <fieldset>
    <legend>
      Your information
    </legend>

    <label for="email">
      Email address
    </label>
    <input id="email"
           type="email"
           autocomplete="email"
           spellcheck="false"
           aria-describedby="hint-email">
    <div class="hint" id="hint-email">
      We’ll never sell or share your information
    </div>

    <label for="phone">
      Phone number
    </label>
    <input  type="tel"
            id="phone"
            inputmode="numeric"
            autocomplete="tel"
            aria-describedby="hint-phone">
    <div class="hint" id="hint-phone">
      Format: 573-268-9692
    </div>

    <label for="message">
      Your message
    </label>
    <textarea id="message"></textarea> 
      

  </fieldset>
</form>