Skip to content

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 ​

ts
import { NuiOtpField } from '@numior/kit';

Usage ​

Change the length of the code, the characters it takes and the states of the field.

Number of characters in the code, 6 by default.

Number of cells in a group between separators. The last group may be shorter.

charset
Characters the field accepts: digits for digits only, alphanumeric for upper-case Latin letters and digits.
size
Changes the size of the control: sm, md or lg. The theme density shifts the height of each size.

Label of the control. Inside NuiField the field provides the label.

Hint below the control. Screen readers announce it with the control.

Error text below the control. Puts the control into the error state.

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.

Six digits from your authenticator app

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. complete does 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.

Keys
Action
←
Moves to the previous cell.
→
Moves to the next cell, but not past the first empty one.
Home
Moves to the first cell.
End
Moves to the first empty cell, or to the last one when the code is complete.
⌫
Deletes the character and moves to the cell on the left. The rest shift left.
⌦
Deletes the character in the current cell. The rest shift left.

API ​

Props

Name
Type
Default
Description
length
number
6
Number of characters in the code, 6 by default.
groupSize
number
—
Number of cells in a group between separators. The last group may be shorter.
charset
OtpCharset
"digits"
Characters the field accepts: digits for digits only, alphanumeric for upper-case Latin letters and digits.
secret
boolean
false
Hides the code behind dots. The character just typed stays visible for a second.
placeholder
string
—
Character shown in empty cells, such as ·. Screen readers skip it.
webOtp
boolean
false
Fills in the code from an SMS through WebOTP until the field is complete.
id
string
—
Id of the control. Defaults to the one from NuiField or is generated automatically.
size
NuiSize
"md"
Changes the size of the control: sm, md or lg. The theme density shifts the height of each size.
disabled
boolean
—
Disables the control. Inside NuiField it defaults to the field value.
readonly
boolean
—
Prevents changes to the value, which stays selectable and copyable. Inside NuiField it defaults to the field value.
required
boolean
—
Marks the control as required. Inside NuiField it defaults to the field value.
invalid
boolean
—
Shows the error state without a text. With error it turns on by itself.
label
string
—
Label of the control. Inside NuiField the field provides the label.
description
string
—
Hint below the control. Screen readers announce it with the control.
error
string
—
Error text below the control. Puts the control into the error state.
reserveError
boolean
false
Reserves space for the error so the form does not shift when it appears.

Models

Name
Type
Default
Description
v-model
string
""
Typed code without separators, from an empty string up to length characters.

Events

Name
Payload
Description
complete
[code: string]
Fires when the whole code is in: typed, pasted, autofilled or received through WebOTP.

Slots

Name
Slot props
Description
separator
{}
Separator between cell groups. A minus icon by default.

Exposed

Name
Type
Description
focus
() => void | undefined
Focuses the field.

Edit this page

Search

Esc