Components
This document describes the available Basis Theory Element components for both JavaScript and React implementations. Each component provides a secure method for collecting sensitive data without exposing it to your application code.
CardElement
A complete payment card input that combines card number, expiration date, and security code fields in a single component.
Properties:
Property | Type | Required | Description |
---|---|---|---|
id / targetId | string | Yes | Unique identifier for the element |
placeholder | object | No | Custom placeholders for each field: {cardNumber, cardExpirationDate, cardSecurityCode} |
style | object | No | Custom styling for the element (see Style Object) |
enableCopy | boolean | No | Enable copy functionality (Chromium browsers only) |
copyIconStyles | object | No | Customize copy icon appearance |
cardBrands | array | No | Limit accepted card brands |
binLookup | boolean | No | Enable BIN lookup |
Implementation:
- Web Elements
- React Elements
// Create the element
const cardElement = bt.createElement('card', {
targetId: 'my-card',
placeholder: {
cardNumber: 'Card number',
cardExpirationDate: 'MM/YY',
cardSecurityCode: 'CVC'
},
autoComplete: {
number: "cc-number",
expirationDate: "cc-exp",
csc: "cc-csc",
},
style: {
base: {
color: "#32325d",
fontFamily: "Arial, sans-serif",
fontSize: "16px"
}
}
});
// Mount the element
cardElement.mount('#card-container');
import { CardElement } from "@basis-theory/react-elements";
<CardElement
id="my-card"
placeholder={{
cardNumber: "Card number",
cardExpirationDate: "MM/YY",
cardSecurityCode: "CVC"
}}
autoComplete={{
number: "cc-number",
expirationDate: "cc-exp",
csc: "cc-csc",
}}
style={{
base: {
color: "#32325d",
fontFamily: "Arial, sans-serif",
fontSize: "16px"
}
}}
/>
Usage Notes:
- Best suited for standard payment forms
- Not recommended for mobile viewports under 400px wide
- Automatically validates and formats card information
TextElement
A general-purpose input for any sensitive string data.
Properties:
Property | Type | Required | Description |
---|---|---|---|
id / targetId | string | Yes | Unique identifier for the element |
placeholder | string | No | Placeholder text |
mask | array | No | Format input with regex patterns and static characters |
transform | regexp or array | No | Transform input before tokenization |
validation | regexp | No | Input validation rule |
validateOnChange | boolean | No | Validate while typing vs. on blur |
maxLength | number | No | Maximum input length |
password | boolean | No | Display as password input (masked characters) |
style | object | No | Custom styling for the element |
Implementation:
- Web Elements
- React Elements
// Create the element
const ssnElement = bt.createElement('text', {
targetId: 'ssn-input',
placeholder: 'XXX-XX-XXXX',
mask: [/\d/, /\d/, /\d/, '-', /\d/, /\d/, '-', /\d/, /\d/, /\d/, /\d/],
transform: /[-]/g, // Remove hyphens when tokenizing
validation: /^\d{3}-\d{2}-\d{4}$/
});
// Mount the element
ssnElement.mount('#ssn-container');
import { TextElement } from "@basis-theory/react-elements";
<TextElement
id="ssn-input"
placeholder="XXX-XX-XXXX"
mask={[/\d/, /\d/, /\d/, '-', /\d/, /\d/, '-', /\d/, /\d/, /\d/, /\d/]}
transform={/[-]/g} // Remove hyphens when tokenizing
validation={/^\d{3}-\d{2}-\d{4}$/}
/>
Usage Notes:
- Suitable for any sensitive data: SSN, bank accounts, addresses, PII, etc.
- Supports sophisticated input formatting via masks
- Can transform data before tokenization
Validation
Basic Patterns
Data Type | Pattern | Example | Description |
---|---|---|---|
Alphanumeric | ^[a-zA-Z0-9]+$ | abc123 | Letters and numbers only |
Credit Card Number | ^\\d{4} \\d{4} \\d{4} \\d{4}$ | 1234 5678 9012 3456 | Credit card with spaces between groups |
Currency | ^\\$\\d{1,3}(,\\d{3})*(\\.[0-9]{2})?$ | $1,234.56 | US currency format with optional decimal |
Email Address | ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$ | test@example.com | Standard email validation |
Password | ^(?=.*[A-Za-z])(?=.*\\d)[A-Za-z\\d@$!%*#?&]{8,}$ | Pass123! | At least 8 chars with at least one letter, one number, and optional special chars |
Social Security Number | ^\\d{3} \\d{2} \\d{4}$ | 123 45 6789 | SSN with spaces between groups |
Tax ID | ^\\d{5}\\d{0,12}$ | 12345 to 12345678901234567 | Tax identification numbers (5-17 digits) |
US Phone Number | ^\\(\\d{3}\\) \\d{3}-\\d{4}$ | (123) 456-7890 | Standard US phone number format with area code in parentheses |
US ZIP Code | ^\\d{5}(-\\d{4})?$ | 12345 or 12345-6789 | US ZIP code (5 digits or ZIP+4 format) |
Implementation Examples
Adding a US Phone Number Validation
textElement.update({
validation: /^\(\d{3}\) \d{3}-\d{4}$/,
mask: [
'(',
/\d/,
/\d/,
/\d/,
')',
' ',
/\d/,
/\d/,
/\d/,
'-',
/\d/,
/\d/,
/\d/,
/\d/,
],
});
Using SSN Validation
textElement.update({
validation: /^\d{3} \d{2} \d{4}$/,
mask: [/\d/, /\d/, /\d/, ' ', /\d/, /\d/, ' ', /\d/, /\d/, /\d/, /\d/],
});
ZIP Code with Mask
textElement.update({
validation: /^\d{5}(-\d{4})?$/,
// Optional extended ZIP code
mask: [/\d/, /\d/, /\d/, /\d/, /\d/, '-', /\d/, /\d/, /\d/, /\d/],
});
Email Address Validation
textElement.update({
validation: /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/,
});
Best Practices
-
Always use anchors: Include
^
(start) and$
(end) anchors to ensure the entire value is validated, not just a portion. -
Match mask and validation: When using both mask and validation, ensure they are compatible with each other.
-
Consider validation timing: Choose appropriate validation timing based on user experience:
validateOnBlur
: Validates when the field loses focusvalidateOnChange
: Validates as the user types
-
Handle optional segments: For patterns with optional segments (like ZIP+4), ensure your mask and validation logic accommodate both formats.
-
Test thoroughly: Test your regex patterns with various inputs, including edge cases.
Notes on Regex Support
When using regex patterns in Elements:
- Escape special characters properly
- Remember that
\d
matches any digit (0-9) - Use quantifiers like
?
(zero or one),*
(zero or more),+
(one or more) appropriately
Input Masking
The TextElement
component supports sophisticated input formatting through the mask
attribute. This feature enables you to control exactly what users can type and how their input is automatically formatted as they type.