Voice Input on a Form Field
Add nds-voice-input to any action button inside a form container to activate speech-to-text. The button toggles listening on and off, and the transcript is written directly into the input.
<div class="nds-form-container nds-search">
<div class="nds-form-control-wrapper">
<div class="nds-form-control">
<i class="nds-icon nds-hgi-search-01 nds-form-leading-icon" aria-hidden="true"></i>
<input type="text" id="search-input-1" class="nds-search-input" name="search"
placeholder="Search services...">
<div class="nds-form-action">
<button class="nds-btn nds-subtle nds-clear" type="button" aria-label="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" type="button" aria-label="Start voice input">
<i class="nds-icon nds-hgi-mic-01" aria-hidden="true"></i>
</button>
</div>
</div>
<div class="nds-form-footer" data-feedback-target hidden></div>
</div>
</div>
Built-in Features
Any button with nds-voice-input inside an NDS form container is wired automatically when the form module loads. No extra JS required.
Reads the page language at recognition start and sets ar-SA for Arabic pages or en-US for English, with no manual configuration needed.
Partial results appear in the field as you speak, styled in italic to distinguish them from committed text. The final transcript replaces them when speech ends.
A short tone plays when the microphone opens (high pitch), closes (low pitch), or encounters an error (very low pitch), giving clear non-visual feedback during dictation.
Listening automatically stops after 30 seconds of inactivity and shows a localized timeout message in the input placeholder, preventing the microphone from staying open indefinitely.
Access NDS.VoiceRecognition directly to create recognition instances, handle transcript callbacks, and wire voice input to custom UI elements outside the standard form container.
Usage Guidelines
Best Practices
- Use voice input on search fields and long free-text inputs where typing is burdensome. Short, constrained fields like phone numbers, postcodes, or PIN codes are not good candidates.
- Use the search input variant (
nds-search-input) for search fields. It includes the microphone button slot alongside the clear button by design. - Do not add voice input to password fields, OTP fields, or other security-sensitive inputs where dictation could expose credentials to bystanders or screen-recording software.
- Do not add voice input to
<select>,<textarea>, or read-only inputs. The plugin targets the primary text input inside the form control and relies on setting.valuedirectly. - Always provide a visible microphone icon in the button so users can identify it without reading the
aria-label. Use thends-hgi-mic-01UI icon for consistency with the rest of NDS. - Graceful degradation is automatic: if the browser does not support the Web Speech API the button is hidden and the input works as a normal text field. Do not write your own feature-detect.
- The plugin requires microphone permission from the browser. Pair it with a visible permission explanation or tooltip when the feature is prominent in a service flow, so users understand why they are being prompted.
- In Arabic layouts the plugin sets
lang="ar-SA"on the recognition instance automatically. You do not need to setdirorlangon the input itself. - For custom integrations using the
VoiceRecognitionAPI directly, always callisSupported()first and guard the rest of your code behind it.
Error Messages
When recognition fails, the plugin sets a localized message as the input placeholder for 3 seconds. These messages are bilingual and chosen automatically by page language.
| Error Code | English Message | Arabic Message |
|---|---|---|
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 | اللغة غير مدعومة |
JavaScript API
The NDS.VoiceRecognition module is exposed for custom integrations. Use it to attach voice input to elements outside the standard NDS form container, or to build custom recording UI with full control over transcript handling.