A Drupal form password confirmation field example

Drupal form FAQ: How do I create a Drupal form with both a Drupal password field and "password confirmation" field?

Earlier I wrote about How to create a Drupal form password field, and assuming that you're comfortable with creating a Drupal form, you can also create a Drupal form with both a password field and password confirmation field very easily.

The Drupal Form API password_confirm element

The Drupal Form API provides a "password confirmation" widget that lets you render both the password and password confirmation fields in one Drupal form field definition:

# the drupal password_confirm field
$form['pass_fields'] = array(
  '#type' => 'password_confirm',
  '#description' => t('Enter the same password in both fields'),
);

That's a basic Drupal form password confirmation field definition. For more control over the HTML that is rendered you can add more Drupal form field definitions to your password field definition, like this:

$form['pass_fields'] = array(
  '#type' => 'password_confirm',
  '#description' => t('Enter the same password in both fields'),
  '#size' => 32,
  '#required' => TRUE,
);

This renders both a password field and password confirmation field that look like this:

Drupal form password and password confirmation fields (password_confirm)

Hopefully most of these Drupal password field options are self-explanatory, but if not, just leave a note in the Comments section below.

You can add even more fields to your Drupal form password and password confirmation fields. The fields you can add are defined in the grid at this Drupal Form API reference page.

Drupal form password confirmation field examples - Summary

I hope this Drupal form password field and password confirmation field example has been helpful. If you have any questions just leave a note in the Comments section below and I'll be glad to provide more information.