Events
You can communicate with Elements by listening to events. When you subscribe to an event, you'll get back a Subscription that you can unsubscribe if/when it fits your workflow.
// Make sure to replace 'event-type' with an actual event type.
var subscription = cardElement.on("event-type", (event) => {
// handle event
});
subscription.unsubscribe(); // stops listening to the event type
On Ready
This event is triggered when the element has rendered and user is able to start interacting with it.
cardElement.on("ready", () => {
// handle ready event
});
On Change
This event is triggered whenever element's value(s) change. For example, if the user types data that doesn't change the state of a field between valid/invalid or empty/filled, you shouldn't expect the event to trigger.
cardElement.on("change", (changeEvent) => {
if (changeEvent.complete) {
// enable submit button
} else {
// disable submit button
// present validation message
}
});
| Parameter | Required | Type | Description |
|---|---|---|---|
event | true | "change" | The event type to listen to. |
handler | true | function | Callback function to be called when the event is fired. Takes in a ChangeEvent. |
ChangeEvent
ChangeEvents are emitted depending on the validation strategy set for each element; by default, validations run onBlur. You can change this behavior by setting the boolean flag validateOnChange to true.
When running validations onChange, elements emit events
whenever an error occurs or if the input's value
changes. Default validation (onBlur) triggers an event every time one of the values below changes:
{
"complete": false,
"valid": false,
"maskSatisfid": false,
"empty": false,
"errors": [
{...},
{...}
],
"cardBrand": "american-express",
"cardLast4": "8431",
"cardBin": "341212"
}
| Attribute | Type | Eligible Elements | Description |
|---|---|---|---|
complete | boolean | All | Whether the input valid and maskSatisfied properties are true. |
valid | boolean | All | Whether the input is valid according to validation for each element. Defaults to true if no validation is defined for the element. |
maskSatisfied | boolean | All | Whether the input satisfies the mask length requirements. Defaults to true if no mask is provided. |
empty | boolean | All | Whether the element is empty. Multi-input Elements will be empty only if all inputs are. |
errors | array | All | Array of FieldError. |
cardBrand | string | card cardNumber | (Optional) The credit card brand (e.g. 'american-express', 'visa', 'unknown'). The value defaults to 'unknown' until a card brand is recognized. |
cardLast4 | string | card cardNumber | (Optional) The credit card's last 4 digits. The value is not provided until a complete card number is entered. |
cardBin | string | card cardNumber | (Optional) The credit card number's first 6 or 8 digits when the input is considered complete. It is 6 digits for card numbers less than 16 digit long and 8 otherwise. |