Creditfield is a small proof of concept module that provides 3 FormAPI elements to be used in the Drupal Form API for custom forms:

  • Credit Card Number
  • Credit Card Expiration Date
  • Credit Card CVV

These fields provide basic validation and form errors on their own. You could easily add more within your own form validation callbacks as well.

To use them in your custom Drupal form, simply define a form item like so:

Drupal 8 & 9

$form['credit_card_number'] = array(
  '#type' => 'creditfield_cardnumber',
  '#title' => $this->t('Credit Card Number'),
  '#maxlength' => 16,
);
	
$form['expiration_date'] = array(
  '#type' => 'creditfield_expiration',
  '#title' => $this->t('Expiration Date'),
);

$form['credit_card_cvv'] = array(
  '#type' => 'creditfield_cardcode',
  '#title' => $this->t('CVV Code'),
  '#maxlength' => 4,
  '#description' => $this->t('Your 3 or 4 digit security code on the back of your card.'),
);

Drupal 7

$form['credit_card_number'] = array(
  '#type' => 'creditfield_cardnumber',
  '#title' => t('Credit Card Number'),
  '#maxlength' => 16,
);
	
$form['expiration_date'] = array(
  '#type' => 'creditfield_date',
  '#title' => t('Expiration Date'),
);

$form['credit_card_cvv'] = array(
  '#type' => 'creditfield_cvv',
  '#title' => t('CVV Code'),
  '#maxlength' => 4,
  '#description' => t('Your 3 or 4 digit security code on the back of your card.'),
);

Where to use Creditfield

Creditfield aims to provide a simple way of collecting credit card fields and validating the input, such as a 'online payment' or 'billpay' form. This does not submit the payment information to any gateway for you- that is up to you in your form processing. If you build a lot of custom payment forms, you might find this useful. If you are looking for a more robust solution, you might want to look into the Webform module with payment handler integrations.

If you build a lot of forms with Form API, and have some that collect payment information, this may likely be useful to you.

Supporting organizations: 
Developed and sponsored by

Project information

Releases