Skip to main content
Login

Sign in to access your account

Text Fields - National Design System

Form fields the user types in, with the label, messages and submit validation that every NDS field shares

Overview

A text field is a .nds-form-container with three parts: a header with the label, a control box with the input, and an action slot for buttons such as Clear. The same container holds an email, password, search, number, textarea or native select field. This page also covers what every NDS field shares: the required mark, the disabled and readonly states, the messages under the field, and submit validation.

Pick another component when:

Markup

Options
Structure
Size
Style
Affix
Affix style
Affix size
State
More
Preview
<div class="nds-form-container"> <div class="nds-form-header"> <label for="full-name"> <span class="nds-label">Full name</span> </label> </div> <div class="nds-form-control"> <input type="text" id="full-name" name="full-name" class="nds-input" placeholder="Enter your full name" autocomplete="name"> <div class="nds-form-action"> <button class="nds-btn nds-subtle nds-clear nds-icon-only" type="button" aria-label="Clear input" data-i18n-aria-label="form.clear_input" hidden> <i class="nds-icon nds-hgi-cancel-01" aria-hidden="true"></i> </button> </div> </div> </div>

Behavior

Field States

Every NDS field uses these states, and the other field pages link here for them. data-required on the container adds the red asterisk, and the forms script adds required to the input. data-state="disabled" or data-state="readonly" on the container sets the same property on every input inside it. The script also works the other way: an input that is disabled, readonly or required in the HTML puts its state on the container.

Submit Validation

A <form class="nds-form"> checks every field when the user submits it. A hidden or disabled field is skipped. A field that fails shows its message under it, the first in page order takes focus, and the submit stops. The API lists each check and its message. A message clears as soon as the user edits the field. A required field whose component has not started yet, such as a Rating or an Upload, fails with “Still loading. Try again in a moment”: see Fields Still Loading. Add data-ajax to stop the page from reloading after a passing submit, and send the request from nds:form:valid.

<form class="nds-form" data-ajax> <div class="nds-flex nds-col"> <div class="nds-form-container" data-required> <div class="nds-form-header"> <label for="applicant-name"> <span class="nds-label">Full name</span> </label> </div> <div class="nds-form-control"> <input type="text" id="applicant-name" name="name" class="nds-input" autocomplete="name"> </div> </div> <div class="nds-form-container" data-required> <div class="nds-form-header"> <label for="applicant-email"> <span class="nds-label">Email address</span> </label> </div> <div class="nds-form-control"> <input type="email" id="applicant-email" name="email" class="nds-input" autocomplete="email"> </div> </div> </div> <div class="nds-form-actions"> <button type="button" class="nds-btn nds-secondary-outline"> <span class="nds-label">Back</span> </button> <button type="submit" class="nds-btn nds-primary"> <span class="nds-label">Submit</span> </button> </div> </form>

A sent form takes no second submit, so a double click sends it once. Its submit button shows the spinner while the form waits:

  • A form with method="post" that loads the next page in the same tab waits until that page opens. Add data-no-pending to a POST form whose answer is a download, or no page: the button would spin until the page reloads.
  • A data-ajax form waits only when the nds:form:valid listener passes its request to event.detail.waitUntil(). The form takes submits again when the request ends, whether it passed or failed. Call waitUntil() in the listener itself, not after an await.

.nds-form draws no box: give the fields a gapped wrapper such as nds-flex nds-col. On an element that is not a <form>, .nds-form checks nothing. Call NDS.Form.validate(step) from your own button there, where step is that .nds-form element, and read valid in the result.

Status Messages

NDS.Form.setStatus() shows a message under a field, such as an error the server returns. A field shows error or help. help is a hint in the neutral color with a “?” icon, not a validation result. Any other status shows the message in the neutral color, with no red outline. setStatus() never moves focus, so focus the first field with an error yourself. Focus also scrolls the field into view.

// Errors the server returned, in field order var errors = [ { field: 'national-id-field', message: 'This ID is not registered' }, { field: 'email-field', message: 'Enter a valid email address' } ]; var first = null; errors.forEach(function (error) { var field = document.getElementById(error.field); if (!field) return; NDS.Form.setStatus(field, 'error', { message: error.message }); if (!first) first = field; }); // A group cannot take focus: focus the first input inside it if (first) (first.querySelector('input, textarea, select') || first).focus();

Permanent Feedback

A .nds-feedback with data-permanent stays under the field as a hint. A validation message hides it, and it comes back when the message clears. It sits in an element with data-feedback-target: every message for the field goes there. A .nds-form-footer puts the messages under the input, and the .nds-form-header puts them above it. Without a feedback target, a message goes right after the field’s input row, above anything else the field holds.

Loading

data-state="loading" on a field shows a spinner button and hides the other action buttons, a suffix included. Use it while a script checks the value, such as a username lookup. The forms script adds the spinner to the field’s last action slot: on Number with Buttons, that is the suffix. A prefix never changes. When loading ends, the buttons come back, and Clear shows only if the field still has a value.

var field = document.getElementById('username-field'); async function checkUsername(name) { NDS.state.add(field, 'loading'); try { var { data } = await NDS.request('/api/username?name=' + encodeURIComponent(name), { json: true }); NDS.Form.setStatus(field, data.available ? 'neutral' : 'error', { message: data.available ? 'This username is free' : 'This username is taken' }); } finally { NDS.state.remove(field, 'loading'); } }

Number Stepper

A text field with min or max on its input takes a whole number in that range. A value typed outside the range is set to the nearest limit when the field loses focus, and the form does not send one. Without min and max, the value has no limit. A blank min or max counts as missing.

The Buttons option adds minus and plus buttons that change the value by step, 1 by default. Hold a button to repeat the step, and hold it about two seconds to step ten at a time. A button at min or max shows a message instead of a change.

Password

The eye button switches the field between hidden and plain text, and its label between “Show password” and “Hide password”. The script hides the password again when the form is sent, by AJAX too, or reset. A form that fails validation keeps it shown. A password field does not accept Arabic letters: the script removes one as it is typed and shows an error.

Built-in Features

  • Auto-initialization

    Every field on the page starts when the page loads, and a field added later starts on its own. The first field on a page that had none needs NDS.init.mount() on the new content. Call NDS.Form.create() only to start a part of the page at once.

  • Inline Messages

    Validation uses the browser's checks but shows no browser popup. The message appears under the field.

  • Left-to-Right Values

    An e-mail, phone or URL field, a nds-formatted field and a Date Picker field read left to right on every page, so a phone number keeps its digit groups in order on an Arabic page. The value stays on the page side, as in the other fields.

  • Custom Checks

    Call input.setCustomValidity('message') to fail a field with your own message, and pass an empty string when it passes. Submit validation blocks the form and shows the message. A message of one space outlines the field with no text.

  • Interaction States

    The script writes focus, active and typing to the container's data-state. A line grows under the field on focus.

  • Clear Button

    The nds-clear button shows only while the field has a value. It empties the field, shows no "required" error, and puts focus back in the field.

  • Screen Reader Errors

    The script sets aria-invalid on a field with an error, and points its aria-describedby at the message. A required field gets aria-required="true".

  • Hidden Fields Skipped

    Submit validation skips a field that is hidden or inside a hidden parent, and a disabled field. A step of a multi-step form that is out of view does not block the submit.

Best Practices

  • Give every field a <label> whose for matches the input’s id. A placeholder is not a label: it disappears when the user types.
  • Write data-required on the container, not required on the input. The container then shows the asterisk before the script loads.
  • Add nds-textarea to a textarea’s container, and nds-select to a native select’s container. Without them, the textarea keeps the single-line height, and the select shows two arrows.
  • Use Custom Select for a choice in a form. Use the native Select only when the design asks for the phone’s own picker.
  • Set data-error-message on an input with a pattern, to say what format it asks for. It shows only when the value does not match the pattern; every other check has its own message.
  • Put a unit, a currency or a short code in a prefix or suffix. The slot also takes a button or a Dropmenu, such as an amount with a currency picker.
  • Use MD only where space is short, such as a table filter. Keep one size in one form.
  • In a row of fields, put help text in a Hint under the field or a Help tooltip beside the label. An Info line under one label pushes that field’s input below its neighbor’s. Use Info in a row only when every field in the row has it. A message under the input, such as an error, moves no input. The audit’s form-row-align check finds a row whose inputs do not line up.
  • Put a form’s Submit and Back buttons in .nds-form-actions, after the fields. The singular .nds-form-action is the button slot inside one field.
  • Set input.value from a script, then call NDS.Form.refresh(input). Setting the value alone does not update the Clear button or the field’s state.
  • To clear a form, call form.reset() or use a reset button. Each NDS field goes back to the value the page shipped with, and its message clears.
  • Give an icon-only action button an aria-label, such as “Clear input”.
  • End a form that needs a confirmation before it sends, such as an application, a booking or a payment, with a review step. The Form Template shows one. Add input steps only when the flow needs them.
  • Keep a simple form, such as a contact form, on one screen. After it sends, show its status or go to the next page.
  • Test each required field type empty, one at a time. A text field, a custom select, a multiselect and a tag input each check their own required state, so one passing proves nothing about the others.

API

Other Classes

Class Element Effect
nds-form <form> Turns on submit validation. It draws no box. On another element it is a marker only, and checks nothing
nds-form-actions a <div> after the fields The row of form buttons, with a top margin. The margin is 0 when the row is the first child. A button that does not fit moves to the next line. It takes the same classes as card actions: nds-end, nds-reverse, nds-col, and nds-col-sm to stack the buttons below 600px
nds-form-footer a <div> at the end of .nds-form-container Holds the field’s messages when it has data-feedback-target. It has no style of its own
nds-required .nds-form-container or .nds-form-group The same as data-required
nds-clear a button in .nds-form-action Empties the field. Write it with hidden: the script shows it while the field has a value
nds-toggle-password a button in .nds-form-action Switches a password field between hidden and plain text. The script adds shown to its data-state while the text is plain
nds-number-decrement, nds-number-increment a button in .nds-form-action Steps the value down or up by step
nds-form-group the <fieldset> around a set of checkboxes, radios or switches See Checkbox, Radio and Switch

Data Attributes

Attribute Element Effect
data-ajax form.nds-form The script stops the page from reloading after a passing submit. Send the request from nds:form:valid
data-no-pending form.nds-form with method="post" A sent form takes submits again at once, and its button shows no spinner. For a form whose answer is a download or no page
novalidate form.nds-form The script sets it at start, so the browser shows no popups
data-required .nds-form-container Marks the field required. The script adds required and aria-required="true" to the input at start. Once the field has started, it also sets or removes this attribute when the input’s required changes, at the next edit or NDS.Form.refresh() call
data-state .nds-form-container Write disabled or readonly, or call setState(): the script sets that property on every input inside. disabled also disables the action buttons. The script writes them too when an input is disabled or readonly. The script sets focus, active and typing while the user is on the field, and removes them on blur and release. It sets open while a native select’s list is open
data-state="loading" .nds-form-container or .nds-form-group Shows the spinner in place of the action buttons. The nds-loading class does the same. Write either in the HTML or from a script (NDS.state.add(), the attribute, classList), and remove it the same way. Put it on the container, not on .nds-form-control or the input
data-status .nds-form-container The script sets it with each message: error, help or neutral. Only error draws the red outline. clearStatus() and any edit to the field remove it
data-message .nds-form-container The script writes the current message text with data-status. getStatus() reads it, and clearStatus() removes it
data-feedback-target .nds-form-container A CSS selector for the element inside the field that holds its messages
data-feedback-target an element inside .nds-form-container, usually .nds-form-footer Messages go in it, instead of right after the input row. A hidden one shows while a message is in it
hidden .nds-form-container, .nds-form-group or .nds-form-action The script removes it at start. Write it to keep a field out of view until its state is set
data-permanent .nds-feedback in the feedback target A message hides it instead of removing it. It comes back when the message clears
data-error-message an <input> with a pattern The message when the value does not match the pattern. NDS.init.audit() flags it on a field with no pattern
data-error-message .nds-form-group, or the container of a field that keeps its value in a hidden input (custom select, multiselect, tag input, upload) The message when nothing is picked. A count limit, a half-filled code and a file still uploading keep their own messages. NDS.init.audit() flags it on a group that may be left empty
inputmode="numeric" the <input> The script blocks a typed character that is not a digit. Arabic digits go in as 0 to 9
min, max a text <input> The range of a whole number. The script sets a value outside it to the nearest limit on blur, and blocks the submit
step the input of a number field The step of the minus and plus buttons
autofocus the <input>, <textarea> or <select> The script focuses the field again at start, without a scroll
data-loading-slot a button in .nds-form-action The button that shows the spinner while the field loads. Without one, the script adds one
data-target .nds-auto-fill The id or name of the input that a suggestion chip fills. A click on an .nds-item writes its text in the input. See Filter
data-autofill-apply .nds-auto-fill A chip click also runs the search: it clicks the search box’s .nds-search-btn, or sends Enter to the input when there is none

Validation Messages

The checks are the browser’s own: write these standard attributes on the input. The forms script shows the message in Arabic or English to match the page.

Check Message
required This field is required
type="email" Please enter a valid email address
type="url" Please enter a valid URL
minlength Input is too short (minimum N characters)
maxlength Input is too long (maximum N characters). The browser stops the user at the limit, so this message rarely shows
pattern Please use the correct format. The value must match the whole expression: pattern="[0-9]{10}" takes exactly ten digits. The message does not name the format, so add data-error-message
min, max Value must be at least N, or Value must be no more than N
any other failure Invalid input. A component that checks its own field, such as Date Picker or Formatted Input, shows its own message

CSS Custom Properties

Property Default Controls
--form-width 100% The width of .nds-form-container. Set it on the container
--input-size 40px The height of .nds-form-control. nds-md sets 32px, and this property has no effect there
--input-radius var(--radius-sm) The corner radius of .nds-form-control and of its prefix and suffix buttons
--actions-margin-top var(--spacing-2xl) The space above .nds-form-actions

Tokens

Set a token at :root, or on a wrapper to reach every field inside it. Give a token with a Dark mode value a dark override too: see Tokens.

Source: the forms group in _sass/tokens/_components.scss.

TokenPreviewValue
--form-field-text-label
var(--text-default)
Dark mode var(--colors-neutral-200)
--form-field-text-placeholder
var(--text-secondary-paragraph)
Dark mode var(--colors-neutral-400)
--form-field-text-hovered
var(--text-default)
Dark mode var(--colors-neutral-200)
--form-field-text-focused
var(--text-primary-paragraph)
Dark mode var(--colors-neutral-300)
--form-field-text-pressed
var(--text-primary-paragraph)
Dark mode var(--colors-neutral-300)
--form-field-text-readonly
var(--text-default)
Dark mode var(--colors-neutral-200)
--form-field-background-default
var(--background-card)
Dark mode var(--colors-neutral-800)
--form-field-background-lighter
var(--colors-neutral-25)
Dark mode var(--colors-neutral-700)
--form-field-background-darker
var(--colors-neutral-100)
Dark mode var(--colors-neutral-900)
--form-field-border-default
var(--colors-neutral-400)
Dark mode var(--colors-neutral-500)
--form-field-border-hovered
var(--colors-neutral-700)
Dark mode var(--colors-neutral-300)
--form-field-border-pressed
var(--colors-neutral-950)
Dark mode var(--colors-neutral-100)
--form-field-border-error
var(--border-error)
Dark mode var(--border-error-light)

It also reads these shared tokens. A change to one also changes other components.

Source: the var() reads in _sass/components/_form.scss.

TokenPreviewValue
--text-display
Read by color

var(--colors-neutral-800)
Dark mode var(--colors-neutral-100)
--text-primary-paragraph
Read by color, --btn-color

var(--colors-neutral-700)
Dark mode var(--colors-neutral-300)
--text-default-disabledvar(--colors-neutral-400)
--border-neutral-primary
Read by background-color

var(--colors-neutral-300)
Dark mode var(--colors-neutral-600)
--border-disabled
var(--colors-neutral-300)
Dark mode var(--colors-neutral-600)
--icon-default-disabled
Read by --btn-color-disabled
var(--colors-neutral-400)
--controls-ripple-effect
Read by box-shadow, background-color

var(--colors-neutral-200)
Dark mode var(--colors-alpha-black-50)
--controls-text-error
Read by color

var(--text-error)
Dark mode var(--colors-red-400)

JavaScript

Method Effect
NDS.Form.init() Starts every field and form on the page. It runs at page load
NDS.Form.create(el) Starts every field and form inside el, and el itself. A <form class="nds-form"> gets submit validation
NDS.Form.setStatus(el, status, options) Shows a message under the field that holds el. status is error, help or any other value, which shows as neutral; an empty value clears the field. Options below. Returns false when el is not in a field
NDS.Form.clearStatus(el) Removes the field’s message and status. A permanent message comes back
NDS.Form.getStatus(el) Returns { status, message, isValid }. isValid is false only for error
NDS.Form.setState(el, name, add) Adds or removes (add: false) a state on the field. 'required' sets data-required and the inputs’ required
NDS.Form.refresh(el) Updates the field after a script sets its value or checked: an input updates its field, any other element every field inside it. It fires no event, except a phone field’s nds:phone:change: see Formatted Input
NDS.Form.validate(el, options) Checks every visible field in the form that holds el, and returns { valid, invalidFields, errors }, in page order. options is { showMessages, focusFirst }, both true by default. Pass both: an object with one key turns the other off
NDS.Form.validateField(el, check, options) Checks one field by its component’s rule and returns its result, or null when no rule answers for el. Each component’s validate(el) calls it: NDS.Checkbox.validate(group), NDS.Radio.validate(group), NDS.Otp.validate(group), NDS.Multiselect.validate(el), NDS.Taginput.validate(el) and the others. A field that shows a message is checked again on each change
Option Default Effect
message — The text. An empty string removes the message and keeps the status
permanent false The message stays as a hint, as data-permanent does
size, style, position none (SM), outline, append Passed to NDS.Feedback.create(). See Feedback
Event Fired on Detail
nds:form:status the field’s container, and it bubbles { status, message }. message is the text the field shows, the same as getStatus(), and '' when it shows none. Both are null after clearStatus()
nds:form:validate the form, and it bubbles { valid, invalidFields, errors }, after every validate(). Each error is { field, input, message }
nds:form:valid the form, and it bubbles { waitUntil(promise) }, when a submit passes. waitUntil() keeps the form from a second submit until the promise ends, and the submit button shows the spinner
nds:form:invalid the form, and it bubbles { invalidFields, errors }, when a submit is blocked
nds:switch:change .nds-switch-toggle, and it bubbles { checked, value, input }. See Switch
nds:checkbox:indeterminate the checkbox, and it bubbles { indeterminate }. See Checkbox
var form = document.querySelector('#apply-form'); // Send the request once every field passes (the form has data-ajax). // waitUntil blocks a second submit until the request ends. form.addEventListener('nds:form:valid', function (e) { e.detail.waitUntil(NDS.request(form.action, { method: 'POST', body: new FormData(form) })); }); // Fill a field from a script var email = document.querySelector('#applicant-email'); email.value = 'name@example.com'; NDS.Form.refresh(email); // Mark a field required after load NDS.Form.setState(email, 'required', true);

The full API is in the banner of _js/nds-form.js.

Last Modified Date: 25/11/2026 - 08:40 PM
Was this page useful?
60% of users said Yes from 2843 Feedbacks