Skip to main content
Login

Sign in to access your account

Voice Input - National Design System

A voice input button types what the user says into a text field, in the page language

Overview

Voice input is a microphone (mic) button for a text field. The user clicks it and speaks. The browser’s speech recognition (the Web Speech API) writes the words into the field. The button sits in the field’s action area, next to the clear button. It can also sit anywhere on the page and name its field.

Pick another component when:

  • the field takes a fixed format, such as a phone number, a postcode or an ID: a plain text field in Forms

Markup

Preview
<div class="nds-form-container"> <div class="nds-form-header"> <label for="voice-search"> <span class="nds-label">Search services</span> </label> </div> <div class="nds-form-control"> <i class="nds-icon nds-hgi-search-01" aria-hidden="true"></i> <input type="text" id="voice-search" name="search" placeholder="Search services..."> <div class="nds-form-action"> <button class="nds-btn nds-subtle nds-clear nds-icon-only" type="button" aria-label="Clear search" data-i18n-aria-label="form.clear_search" hidden> <i class="nds-icon nds-hgi-cancel-01" aria-hidden="true"></i> </button> <button class="nds-btn nds-subtle nds-voice-input nds-icon-only" type="button" aria-label="Start voice input" data-i18n-aria-label="voice-input.start_voice_input" aria-pressed="false"> <i class="nds-icon nds-hgi-mic-01" aria-hidden="true"></i> </button> </div> </div> </div>

Behavior

Linked Button

A button inside .nds-form-control dictates into that field’s input or text area. A button anywhere else names its field in data-voice-target. The script finds the field on each click, so the field can load after the button. While the mic is open, the button’s icon does not change color: the listening state is on the field’s container, and the button is outside it.

Built-in Features

  • Auto Start

    The loader starts voice input when the page has a nds-voice-input button. One click listener on the document serves every button, including buttons added later.

  • Page Language

    Each time the mic opens, the script reads lang on <html>. Recognition uses Arabic (ar-SA) when it is ar, and English (en-US) for any other value. The field needs no lang of its own.

  • Live Transcript

    Words show in the field while the user speaks. They go in at the caret, or over the selected text, and the rest of the text stays. A field the user was not in takes them at the end. The final text fires input and change, so a form treats it like typed text.

  • Mic State

    While the mic is open, the button has aria-pressed="true" and the label "Stop voice input". A mic icon inside the field cycles color. The field gets the focus.

  • Audio Tones

    A high tone plays when the mic opens, a low tone when it closes, and a lower tone on an error.

  • Time Limit

    The mic closes 30 seconds after it opens, and the field shows a timeout message. It also closes when focus leaves the field, when the form is sent, or when the button is removed from the page.

  • Error Messages

    An error shows under the field for 4 seconds, in the page language, and screen readers announce it. On a browser without speech recognition, a click shows a "not supported" message and the field still takes typed text.

Best Practices

  • Use voice input on search fields and long free-text fields. Typing these takes the most effort.
  • Never add it to a password, OTP or other secret field. People near the user can hear what they say.
  • Do not point it at a read-only field or a <select>. A read-only field still takes the text, and a <select> loses its choice.
  • Write aria-label="Start voice input" and aria-pressed="false" on the button.
  • Keep the nds-hgi-mic-01 icon as an <i> directly inside the button. The color animation styles only that element.
  • The browser asks for permission to use the mic the first time. When voice input is a main part of a service, say why in the info text.
  • To hide voice input on a browser without speech recognition, remove the button when NDS.VoiceInput.isSupported() returns false. The JavaScript example below does this.
  • For the label, info text, feedback and the required mark, see Forms. They work the same on every field.

API

Data Attributes

Attribute Element Effect
data-voice-target .nds-voice-input The id, name or data-name of the field to dictate into. With it, the button can sit anywhere on the page
aria-pressed .nds-voice-input Write false at page load. The script sets true when the mic opens, and false when it closes
aria-label .nds-voice-input Write “Start voice input” at page load. The script sets “Stop voice input” when the mic opens. When it closes, the script sets “Start voice input” with the language name, such as “Start voice input (English)”. Both labels are in the page language
data-name the target input or textarea The script matches the data-voice-target value against it when no id or name matches
data-state~="listening" .nds-form-container of the target field The script adds it when the mic opens, and removes it when the mic closes. It makes the mic icon cycle color

JavaScript

The NDS.VoiceInput script loads after the page shows. Until it loads, each call loads it and returns a promise of the result, so await a call whose result you use.

Method Effect
NDS.VoiceInput.init() Adds one click listener to the document for every voice button. The loader calls it on load. A second call does nothing
NDS.VoiceInput.isSupported() Returns true when the browser has speech recognition

Voice input fires no events of its own. When the final text lands, the field fires input and change. The full API is in the banner of _js/nds-voice-input.js.

// Remove the buttons on a browser without speech recognition if (!NDS.VoiceInput.isSupported()) { document.querySelectorAll('.nds-voice-input').forEach(function (button) { button.remove(); }); } // A dictation fires change, like typed text document.getElementById('voice-search').addEventListener('change', function (e) { console.log(e.target.value); });

Messages

The script shows these under the field for 4 seconds, in the page language. They are neutral, so the field is not marked invalid. While one shows, it takes the place of the field’s validation message, which comes back after.

Code English Arabic
no-speech No speech detected لم يتم اكتشاف صوت
not-allowed Microphone permission required مطلوب إذن الميكروفون
audio-capture Microphone access denied تم رفض الوصول للميكروفون
network Network error خطأ في الشبكة
aborted Voice input cancelled تم إلغاء إدخال الصوت
language-not-supported Language not supported اللغة غير مدعومة
timeout Voice input timed out انتهت مهلة إدخال الصوت
unsupported Voice input is not supported in this browser إدخال الصوت غير مدعوم في هذا المتصفح
Any other code Voice input error خطأ في إدخال الصوت
Last Modified Date: 01/11/2026 - 05:15 PM
Was this page useful?
60% of users said Yes from 2843 Feedbacks