Forms

Credit card

A picture of a payment card that follows the card fields as the user types (number, cardholder name, expiration and security code), with the brand detected from the number, plus the fields themselves, ready to drop in a checkout.

You want Use
Only the card tk:credit-card
The card with its fields, laid out tk:credit-card.form
Only the fields tk:credit-card.fields

The card never binds to anything: it finds the fields by their autocomplete token (cc-number, cc-name...) and reads them. The fields are ordinary tk:input and tk:select, so they bind, validate and show errors like any other.

Blade

The card follows the fields of the closest <form>. The inputs, their names and their layout are yours: the card reads any field with a card autocomplete token, whatever its name and whether it is a tk:input or not, and tk:input gives these tokens their masks. A split expiration works too: a field with cc-exp-month and one with cc-exp-year are combined, and a missing part shows as MM or YYYY. Without a <form> around, give the element that holds the fields an id and the card for with it (<tk:credit-card for="payment-fields" />).

Outside a form and with no for, the card shows only the values given in options, as for a saved card:

A number with anything other than digits and spaces is printed as given, so a masked number keeps its dots. type sets the brand when the number alone cannot tell it.

The card with its fields #

The card, then "Card number" and "Cardholder name" on full rows, then "Month", "Year" and "Security code" side by side. Typing 3782 8224 6310 005 shows 3782 822463 10005 on the card with the American Express logo and colors; choosing 04 and 2030 shows 04/2030. Focusing the security code turns the card to its back, and focusing any other field turns it to the front. The number takes up to 19 digits in groups of four, the security code up to 4 digits, the name up to 26 characters, and the year lists the current year and the next ten.

name nests the fields under one key (payment[number], payment[expiration_month]...), and inside a Livewire component that has the property each one binds itself to the same path. After the user fills them in, $payment holds ['number' => '4111 1111 1111 1111', 'holder_name' => 'Jane Doe', 'expiration_month' => '04', 'expiration_year' => '2030', 'cvv' => '123']. required marks all of them. Without name, the keys are the field names alone (number, holder_name...), and each field binds to the component property with that name ($number, $holder_name...) when there is one. With expiration="input" the month and year selects become one MM/YY input, with a mask, and the key is expiration ('12/30').

Received: —

Blade

Only the fields #

The same fields as tk:credit-card.form, with the same props, keys and binding, without the card. To show the card in a place of your own, add a tk:credit-card in the same <form>, here on the left of the fields:

Received: —

Blade

Expiration as one input #

expiration=input replaces the month and year selects with one MM/YY input, with a mask, sent as expiration.

Blade

Fields outside a form #

for points the card to another element, by id or CSS selector. Split month and year fields (cc-exp-month, cc-exp-year) are combined into one expiration.

Blade

Other brands and the back #

Other brands from type, and back in options to start on the back face, with the cvv.

Blade

Flipped event #

flipped fires on the card with { back } in the detail each time it turns over. Click the card to flip it; it does not bubble, so listen on the tag.

Showing: , flips: 0
Blade

Custom face #

The front-svg slot replaces the drawing of the front. It sits in the card scope, so it can print values.holderName and the formatted displayNumber().

Blade

Customization #

Classes and attributes go to the card element; class=max-w-xs replaces the default width and aria-label names the card.

Blade

Reference

tk:credit-card              the card alone
tk:credit-card.form         the card with its fields
├── tk:credit-card
└── tk:credit-card.fields
tk:credit-card.fields       the fields alone

tk:credit-card

Prop Type Default Description
for string the closest <form> The id of the element whose fields the card follows, or a CSS selector for it.
options array null Initial values and settings, below.

Keys of options:

Key Default Description
number null The card number shown before a field provides one.
holderName null The cardholder name.
expirationDate null The expiration, shown as given (12/30).
cvv null The security code, shown on the back.
type detected Forces the brand: americanexpress, diners, discover, elo, hipercard, jcb, maestro, mastercard, unionpay or visa. Any other name shows no brand.
back false Starts with the back of the card showing.
for null As the for prop, which wins when both are given.

Slots

Slot Description
front-svg Replaces the drawing of the front.
back-svg Replaces the drawing of the back.

Events

Event Detail When
flipped { back }, true when the back is now showing The card turned over, by a click, the Flip card button, or the focus moving between the security code and another card field. Not fired when the side does not change.

tk:credit-card.form

Prop Type Default Description
name string null Nests the fields under this key: payment gives payment[number], payment[holder_name], payment[expiration_month], payment[expiration_year] and payment[cvv].
expiration string select select asks for the expiration in a month select (01 to 12) and a year select (this year and the next ten); input asks for it in one MM/YY input, sent as expiration.
required bool false Marks every field as required.
preview bool true false leaves the card out.
size string md Passed to the fields.

The wrapper gets an id (yours, or one made from name), and the card follows the fields inside it, whether or not there is a <form> around.

Slots

Slot Description
default Content after the fields, inside the grid (a submit button, a note).

tk:credit-card.fields

Prop Type Default Description
name string null As on tk:credit-card.form.
expiration string select As on tk:credit-card.form.
required bool false As on tk:credit-card.form.
size string md Passed to the fields.

The fields, with their labels (translated) and tokens:

Key Label autocomplete Field
number Card number cc-number input, masked to 19 digits in groups of four
holder_name Cardholder name cc-name input, up to 26 characters
expiration_month Month cc-exp-month select, 01 to 12
expiration_year Year cc-exp-year select, this year and the next ten
expiration Expiration cc-exp input with expiration="input" instead of the two selects, masked to MM/YY
cvv Security code cc-csc input, up to 4 digits

Slots

blade

The content sits inside the card's Alpine scope, so it can print values.number, values.holderName, values.expirationDate, values.cvv and the formatted displayNumber(). The brand logo is still placed over the top corner of the front.

Events

flipped is dispatched on the card element and does not bubble: listen on the tag itself.

With Alpine:

blade

Calling a Livewire action:

blade

Customization

Classes and attributes go to the card element. Its classes are merged with the defaults, so class="max-w-xs" replaces the default max-w-[400px]. The component has no prefixed attributes; use the front-svg and back-svg slots to change the faces.

blade

On tk:credit-card.form, classes and attributes go to the wrapper, a grid; it gets an id (yours, or one made from name), which its card follows. To reach an inner element, prefix the attribute:

Prefix Tag Goes to
preview: tk:credit-card.form The card.
number: tk:credit-card.form, tk:credit-card.fields The card number input.
holder: tk:credit-card.form, tk:credit-card.fields The cardholder name input.
expiration-month: tk:credit-card.form, tk:credit-card.fields The month select.
expiration-year: tk:credit-card.form, tk:credit-card.fields The year select.
expiration: tk:credit-card.form, tk:credit-card.fields The expiration input, with expiration="input".
cvv: tk:credit-card.form, tk:credit-card.fields The security code input.

number:field:class and holder:field:class are added to the classes that make those inputs span the whole row.

blade

Accessibility

The card is a role="group" named "Credit card" (translated), followed by the detected brand ("Credit card Visa"); an aria-label of your own replaces the first part. Both faces are aria-hidden: the values are read from the inputs, not from the picture. A visually hidden "Flip card" button with aria-pressed (true while the back shows) turns the card over, and the card shows a focus ring while that button has keyboard focus.

Key Action
Tab Reaches the Flip card button.
Enter, Space On the Flip card button, turns the card over.

Behavior notes

  • Fields are found by autocomplete token, not by name: cc-number, cc-name, cc-exp, cc-csc, and cc-exp-month with cc-exp-year for a split expiration. A token may come with others (autocomplete="billing cc-number").
  • When a field is found, its value replaces the one from options, even when it is empty: inside a form with an empty cc-number input, options.number is never shown. Use options for a card that has no fields.
  • The brand is detected from the leading digits and sets the logo and the card colors. American Express numbers are grouped 4-6-5, Diners Club 4-6-4 and the others 4-4-4-4; digits beyond the groups are kept at the end.
  • The number shows at most 19 digits, the expiration 7 characters and the security code 4. A long number or expiration is squeezed to fit its place on the card; a long cardholder name is cut with an ellipsis.
  • tk:input fields with the cc-number, cc-exp and cc-csc tokens get matching masks, and cc-name a 26-character limit: see Input.
  • A card that is hidden when its values arrive (in a closed tab or modal) fits its texts again once it is shown.
  • The masks format what the input holds, and that is what wire:model and the form send: the number arrives with its spaces (4111 1111 1111 1111), and an expiration input as MM/YY.
  • Resetting the form puts the card back to what the fields hold after the reset.
  • The card has wire:ignore: a Livewire update does not touch it, and options is read only once. The values still follow the fields after every update.
  • Clicking anywhere on the card turns it over, including on content of the custom face slots.
  • From Alpine code inside the card's scope, update({ number, holderName, expirationDate, cvv, type, back }) changes the shown values: the keys given replace the current ones, type forces the brand and back turns the card to that face. When the card follows fields, the next input in any of them reads all the fields again and replaces those values.