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
Variants
| Group | Option | Markup | On element | Use |
|---|---|---|---|---|
| Structure | Search field (default) | — | — | A field with a search icon, a clear button and the voice button. The most common use |
| Structure | Text area | canon #voice-textarea |
— | Long free text, such as a message. Each dictation adds at the caret |
| Structure | Search box | canon #voice-search-box |
— | A search field with a Search button, at the top of a results page |
| Structure | Linked button | canon #voice-linked |
— | The button sits outside the field and names it in data-voice-target |
| Field states | Label, info, feedback, required | — | — | Shared by every form field. See Forms |
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-inputbutton. One click listener on the document serves every button, including buttons added later. -
Page Language
Each time the mic opens, the script reads
langon<html>. Recognition uses Arabic (ar-SA) when it isar, and English (en-US) for any other value. The field needs nolangof 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
inputandchange, 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"andaria-pressed="false"on the button. - Keep the
nds-hgi-mic-01icon 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()returnsfalse. 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.
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 | خطأ في إدخال الصوت |