OTP field
A field for a one-time code from an SMS, an email or an authenticator app.
Sent to peter.wolfe@example.com
Set the number of characters in length and the size of the groups in groupSize.
Installation
import { NuiOtpField } from '@numior/kit';Usage
Change the length of the code, the characters it takes and the states of the field.
Examples
Confirm a sign-in by SMS
Send the code for checking in the complete handler.
Sent to (617) 555-0101
123456 is your code to sign in to the store dashboard. @example.com #123456
On a phone the code from the SMS appears above the keyboard, and one tap fills the field.
Add web-otp, and Chrome on Android fills in the code by itself once the user allows it. The last line of the SMS must look like @example.com #123456, where example.com is your domain.
Accept a backup code
Allow letters with charset.
Eight characters from the recovery card
The field takes Latin letters and turns them upper case.
Hide a PIN
Add secret.
Four digits under the scratch-off strip
The character just typed stays visible for a second. Browsers treat the field as a password and do not suggest SMS codes for it.
Check the code in a form
Wrap the field in NuiFormField.
The required() rule catches an empty code, and min(6) an incomplete one.
Behavior
- A typed character goes into the current cell, and the caret moves to the next one. A click on a filled cell selects it, so the next character replaces it.
- A pasted code of full length replaces the old one, without spaces and dashes. A shorter paste fills the cells from the current one.
- Deleting a character shifts the ones after it to the left, so the code never has gaps.
- The model holds the code without separators, with digits of any script turned into 0–9.
completedoes not fire when the app sets the value itself.
Accessibility
A screen reader sees one text field with its label, hint and error, and the cells are hidden from it. A field with secret is announced as a password. In right-to-left text the cells still run left to right, like the digits of the code.