<x-widget.verification-code>
Verification code Blade component for Laravel and Livewire
A single field for a verification code: digits only, or letters too, cut to its length. The better choice for a code sent by text or email: the phone can autofill it, it pastes in one go, and screen readers read it as one field. See the OTP for one box per character. Works with Livewire wire:model.
php artisan bladewell:add verification-code
- Works with
- Laravel 12 and 13, Livewire 3 and 4, Tailwind CSS 4.1+, PHP 8.3+
Usage
Livewire
In a Livewire component, bind with wire:model (deferred) or wire:model.live; no name is needed. The property only ever gets the allowed characters, cut to the length, so with .live updatedCode() can verify as soon as strlen($this->code) reaches it. Set the property to '' in PHP, after a wrong code, and the field empties.
<x-widget.verification-code label="Verification code" :length="6" wire:model.live="code" />
On the server
The verification code can't stop a request that skips it. Here's what to check on your server, and what to keep cheap.
Security
-
Store the code hashed, as you would a password, with an expiry (10 minutes is common). To use it, check it with
Hash::check()(never==), then mark it used in one conditional update that only succeeds while it's unused:Code::whereKey($code->id)->whereNull('used_at')->update(['used_at' => now()]) === 1. Two submissions at once can both pass the check; only one wins the update, so the code works once. -
Consider allowing a few tries per code (5 is common) and then asking for a new one, and throttling by account as well as IP: a 6-digit code is only a million guesses.
Server cost
-
Each resend is an email or SMS, and SMS is billed per message. Consider one resend a minute and a daily cap per account and per phone number, which also stops people using your form to send texts to numbers they don't own.
Examples
Verification code
A single field for a code: digits only (or letters too, with alphanumeric), and no longer than its length. A pasted "123-456" keeps just the digits. Once it holds a whole code it fires verification-code:complete, for a form that submits itself.
We sent a 6-digit code to ••• 4821.
Show code Hide code
<x-widget.verification-code name="code" label="Verification code" info="We sent a 6-digit code to ••• 4821." class="max-w-sm" />
Lengths and letters
length sets how many characters it takes (6 by default); anything past it is dropped. alphanumeric takes letters as well as digits, for backup and invite codes.
Show code Hide code
<div class="grid gap-6 sm:grid-cols-3">
<x-widget.verification-code name="pin" label="4-digit PIN" :length="4" />
<x-widget.verification-code name="sms_code" label="8-digit code" :length="8" />
<x-widget.verification-code name="backup_code" label="Backup code" :length="8" alphanumeric />
</div>
States
The error clears the moment the user edits the code.
That code has expired. Request a new one.
Show code Hide code
<div class="grid gap-6 sm:grid-cols-3">
<x-widget.verification-code name="code_error" label="With error" value="482" error="That code has expired. Request a new one." />
<x-widget.verification-code name="code_readonly" label="Read-only" value="482913" readonly />
<x-widget.verification-code name="code_disabled" label="Disabled" disabled />
</div>
Props
Other attributes, such as autocomplete or data-*, are passed through to the element. class styles the component's outer wrapper.
<x-widget.verification-code>
| Prop | Default | Description |
|---|---|---|
| name |
null
|
What it submits as; its error and old input are found under it. Optional with wire:model, which then names it. |
| id |
null
|
Defaults to one made from the name (or the wire:model property). |
| label |
null
|
Shown above the field, and its name for screen readers. |
| value |
null
|
The value to show. Old input wins after a failed submit; with wire:model and no value, the bound Livewire property. |
| placeholder |
null
|
Shown while it's empty. |
| error |
null
|
An error message of your own; otherwise the validation error for the name, from the session or Livewire. |
| info |
null
|
A hint under the field. |
| bag |
'default'
|
Which error bag to read the error from. |
| disabled |
false
|
Greyed out: it can't be changed. |
| readonly |
false
|
Shown, and submitted, but it can't be edited. |
| length |
6
|
How many characters the code has; extra ones are dropped. |
| alphanumeric |
false
|
Letters and digits instead of digits only. |
Accessibility
All 3 examples above are checked with axe-core against the WCAG 2.2 A and AA rules, in the light theme and the dark one, both as the page draws and with each popover, dialog, toast and tooltip opened, on every change. A change that fails can't be merged. Where axe can't decide, such as contrast on SVG text, the test measures the colours itself instead of letting it pass.
Automated checks can't judge everything: how it sounds in a screen reader, and how it feels to use from the keyboard, still need a person. Check those on your own pages too.
Source
What bladewell:add verification-code writes to your app with the default namespaces. Prefer to copy by hand? Take these files, plus the ones from
Field, and the theme and base CSS.
resources/views/components/widget/verification-code/index.blade.php Show
resources/js/widget/verification-code/index.js Show
app/View/Widget/ElementIds.php Show
app/View/Widget/FormField.php Show