Skip to Main Content

Input fields

Input fields let users share information required in order to complete a task.

Use when:

  • Users need to enter information that is best communicated in text form.
  • You need to collect information that varies from one user to another and can’t be represented as a set of pre-determined choices.

Interactive component

Error message goes here

Hint text goes here

200 Character limit here

200 Character limit here

Error message goes here

Code Samples

Basic

Note that this component may require additional styling.

Angular

<matInput> is a directive that allows native <input> and <textarea> elements to work with <mat-form-field>.

Note that this component may require additional styling.



Mendix

To create an Input Field, use Textbox and/or Text area elements from the toolbox on the right.

The styles for these elements should be automatically applied along with the styles for their error states.

The Mendix toolkit can be found here.

Library integration

Use the codes below to add this component to your project. Download the latest version of the library to easily implement our full range of components.

Angular

Step 1. Install

Execute the following command to install this component.

npm i [NPM-PLACE-HOLDER]/ds-input-field

Step 2. Import

Import the following into your app.module.ts file.

import { DsInputFieldModule} from 'ds-input-field';

Code snippets

Input field
<cpc-ds-input-field
    labelText = "Full name"
    fieldValue = "This is text"
    [formControl]="fullNameFormControl"
    id = 'fullName'>
</cpc-ds-input-field>
Disabled
<cpc-ds-input-field
    labelText = "Full name"
    fieldValue = "This is text"
    [formControl]="disableFullNameFormControl"
    id = 'fullName'>
</cpc-ds-input-field>
Readonly
<cpc-ds-input-field
    labelText = "Full name"
    fieldValue = "This is text"
    [readOnlyField] = "true"
    [formControl]="fullNameReadOnlyFormControl"
    id = 'fullName'>
</cpc-ds-input-field>
Hint text
<cpc-ds-input-field
    labelText = "Full name"
    hintText = "Don't disclose personal infos"
    fieldValue = "This is text"
    [readOnlyField] = "false"
    (click)="beep()"
    [formControl]="fullNameFormControl"
    id = 'fullName'>
</cpc-ds-input-field>
Password
<cpc-ds-input-field
    labelText = "Password"
    type="password"
    id = 'password'>
</cpc-ds-input-field>
Textarea
<cpc-ds-input-field
    id = 'additionalDetails'
    inputFormat = "textAreaType"
    [formControl]="textareaFormControl"
    labelText = "Additional Information"
    [showMaxLengthLabel] = "true"
    [maxchar] = "140">
</cpc-ds-input-field>
Properties
Name Type Default Description
id string id

id for the input field

inputFormat string  

There are two formats available:

  • Text field
  • Text area

Library will show a text field by default. If you want textarea, use inputFormat “textAreaType”

name string txt

The name of the input field

type string text

There are 2 types available:

  • text
  • password
labelText string  

Text for the label

hintText string  

Hint text for the field

readOnlyField boolean false

Make field read-only

showMaxLengthLabel boolean false

Show the maximum length available in textarea

maxchar number 120

The number of characters allowed in textarea

formControl Form control  

FormControl for the input field

fullNameFormControl = new FormControl({ value: 'TEST', disabled: false }, [Validators.required]);

(click) event  

Call your method click event

(click)="beep()"

HTML

Step 1: Import CSS

Import the following into your theme or page.

<link type="text/css" href="[CSS-PLACE-HOLDER]" rel="stylesheet">

Step 2: Import JS

<script src="[JS-PLACE-HOLDER]"></script>

Code snippets

Input field
<label for="fullname">Full name</label>
<input id="fullname" type="text" autocomplete="off">
Input field with error
<label for="error">Full name *</label>
<input
 aria-required="true"
 aria-invalid="true"
 aria-describedby="errMsg2"
 id="error"
 type="text"
 class="error"
 autocomplete="off">
<div id="errMsg2" class="ds-error">Error message goes here</div>
Hint text
<label id="fullnameHint">Full name</label>
<p id="hintText" class="hintText">Hint text goes here</p>
<input
 type="text"
 autocomplete="off"
 aria-labelledby="fullnameHint hintText">
Readonly
<!-- Read Only -->
<label for="fullnameReadonly">Full name</label>
<input
 id="fullnameReadonly"
 readonly
 value="Read only text here"
 type="text"
 autocomplete="off">
Disabled
<label for="disabled">Full name</label>
<input
 aria-disabled="true"
 disabled
 id="disabled"
 class="disabled"
 type="text"
 autocomplete="off">
Textarea
<label 
 for="textarea" 
 id="textarea-label">
  Additional Information
</label>

<textarea 
 id="textarea" 
 maxLength="102" 
 aria-labelledby="textarea-label textarea-maxCharacter">
</textarea>

<p 
 id="textarea-maxCharacter" 
 class="maxCharacter">
  102 Character limit here
</p>
Textarea with error
<label
 for="textarea2"
 id="textarea2-label">
  Additional Information
</label>

<textarea
 aria-invalid="true"
 aria-describedby="errMsg"
 aria-labelledby="textarea2-label textarea2-maxCharacter"
 id="textarea2"
 class="error"
 maxLength="10">
</textarea>

<p
 aria-live="polite"
 id="textarea2-maxCharacter"
 class="maxCharacter">
  10 Character limit here
</p>

<div
 id="errMsg"
 class="ds-error">
  Error message goes here
</div>

General guidelines

Input fields enable users to submit unique or complex data.

Short input fields are useful for names, addresses or any other piece of information that is unique from user to user.

Long input fields (known as text areas) are ideal for collecting qualitative responses or complex data.

When a user is typing in a password, it should be hidden, unless a user chooses to reveal it.

Positioning

Consider the amount and type of information being asked for when determining the length of the field. Align labels to the top and left of the input field to let users scan quickly.

Input fields are large components and will generally take up most of a form. This is an important consideration when structuring forms.

  • Keep in mind that input field size is powerful visual language. Large fields suggest an expectation of large responses, small fields encourage small responses.

Helper text

Helper text is a powerful way to influence input field responses, but not always helpful. Consider the value of the helper text you include in your design. If you can’t make a confident case for including some, remove it!

Helper text

Interaction

States

Input fields have default, focus, active and error states.


  1. Default
    In this state, the user hasn’t engaged with the field but is able to.
  2. Focus
    In this state, a user has navigated to the input field without a mouse. The component is surrounded by a blue box and can be activated by entering text or pressing spacebar.
  3. Disabled
    In this state, an input field is visible but cannot be interacted with. Faded colours are used to distinguish disabled input fields from active ones.
  4. Read-only
    In this state, the content of an input field is not interactable. This state is typically used when an input field is filled by a backend process (SKU, order number, etc.).

Default

Default

Focus

Focus

Disabled / Read-only

Focus

Feedback

Error messages

Error messages go beneath the input field.

Error messages

On form validation, error messages are shown when user has provided incorrect information or violated at least 1 requirement. Provide context so the user can correct their errors.

Placeholder

If a user must input information according to a specific format, make sure to indicate that clearly. Don’t rely exclusively on placeholder text as it disappears quickly and is not always read by assistive technologies. Instead, explain formatting requirements using hint text below the field.

Many input fields support a variety of input formats, automatically translating data into a formal structure upon submission. For example, when inputting a postal code, users can use any combination of uppercase, lowercase, space, no-space and hyphenated inputs. This data is validated after submission, ‘translated’ to the proper format, and then captured by your form. In cases like these, there is no need to indicate a requirement for specific formatting.

Focus
  • Whenever possible, leave your fields as open as you can. If form validation can handle errors on the backend, don’t burden your design by also handling them on the front end.

Accessibility considerations

Screen readers

Use labels to ensure input fields are announced when brought into focus.

Placeholder text

Assistive technologies do not treat placeholder text as labels. Some are not supported and not displayed in older web browsers. It may even make users think that the form input is already filled out.

Use hint text over placeholder text whenever possible. Provide the information that the user needs to fill out the form.

Design specs

Sizes and spacing

General guidelines

Write input field labels in plain language. Inform the users what they should be typing into the fields.

  Guidelines Examples
Recommended length 1 to 3 words for labels Date
Line wrap Avoid Name
Phrasing A noun, or a group of words that act as one Promo code
Case Sentence case Additional pickup instructions

Dos and don’ts

Required and optional fields

For forms that consist of five or more different input fields that are a mix of required and optional fields, it’s helpful to add the following statement.

“All fields are required, unless indicated as optional.”

For fields that are optional, add the word “(optional)” in parentheses next to the input field label.

  • Description (optional)
  • Optional: Postal code

Best practices

  • An input field is used for single-line text input.
  • Each input field should be preceded by a label element to describe the expected input.
  • Placeholder text can be leveraged as an aid to user input.

Examples

Use examples to complement input field labels when further clarity would be helpful. The additional microcopy can demonstrate how to format field entries and can help users quickly visualize what they need to type. Giving users examples can improve efficiency and help them avoid errors.

  • Customer number
    Example: 1234567
  • Customer number

Error messages

Write error messages that focus on directions and how users can quickly fix the problem to move forward. Error messages should be used to educate users, rather than only deliver negative feedback. The tone of error messages should be polite and constructive. You can offer an alternate solution through a link, if it’s possible that the user needs something else. Avoid error message codes, use plain language and minimize the text.

  • We can't access or change the default credit card information.

Accessibility considerations

Accessible error messages

Use accessible error messages that are visible to assistive technology and link users to the corresponding entries. If multiple errors apply, add an overview accessibility error message that links users to the applicable errors.

Overview accessible error message

 

Accessible error message

Guidelines

Keyboard interaction

When the input field has focus:

  • Tab: Moves keyboard focus to the next interactive element on the page
  • Shift + Tab: Moves keyboard focus to the previous interactive element on the page
  • Character key: Prints the key in the text box
  • Left arrow: Moves to the previous character in the input field
  • Right arrow: Moves to the next character in the input field

Autocomplete input fields

Autocomplete attributes help users with limited dexterity and cognitive disabilities. A complete list of HTML 5.2 autofill input type purposes is available courtesy of W3C.

Communicating text area character limits

Screen reader users need to be notified of a text area’s character limit. This is best achieved by including:

  • A visible character counter. The visible character counter string can also be associated with the label using aria-labelledby.
  • A string injected at specific moments with aria-live=”polite” that updates screen reader users on how many characters remain (for example, 0 characters left), maximum characters reached (75%, 50%, 25%, 10%), characters left, and so on.

ARIA roles and properties

The following WAI-ARIA roles and properties can be associated with input fields and text area elements:

  • Use a native HTML <input> with type=”text” for single line input or an HTML <textarea> tag for multi-line input. The attribute role="textbox" can be added to the element when designing a customized <input> field.
  • Aria-required=”true” must be set to programmatically communicate mandatory fields to screen reader users.
  • Aria-invalid=”true” for a field in error.
  • Aria-disabled=”true” for a disabled field.
  • "readonly" property on a readonly field.
  • Aria-live=”polite” on the character count text string should be announced to screen readers. The string is nullified on next key-press.
  • Field label, hint text (if present), and character count string (if present) are associated with the field using aria-labelledby. The attribute takes a space separated value of the IDs of the elements containing each string.
  • Error text (if present) is associated with the field using aria-describedby. The attribute takes the ID of the error string element as value.

Testing

When testing an input field, the tester must ensure:

  • The input field is fully keyboard operable
  • Indications for the focused state are clear and prominent
  • ARIA properties are correct
  • Visible indicators of required fields (for example, “*”) are explained at the top of the form
  • Character limits (if present) are announced to the screen reader
  • The screen reader announces the field label, field role information, hint text (if present), error text (if present), character count (if present), and announcements for any associated ARIA properties (for example, “required” or “invalid”)
  • The correct keyboard type is present for mobile users (for example, the email keyboard for email fields)

Related content