Skip to content
Laryn - CEDC.org edited this page Nov 12, 2019 · 2 revisions

Welcome to the maxlength wiki!

Webforms

MaxLength works in combination with Webform Validation. Create a "maximum length" validator and MaxLength will automatically kick in.

Code/API Usage

Whether when creating a form or when altering one, put both #maxlength and #maxlength_js properties on the elements you want to control.

#maxlength

Description: The maximum amount of characters to accept as input.

Values: A positive number.

#maxlength_js

Used by: textfield, textarea, text_format

Description: Indicates whether or not the field should have a reverse character counter.

Values: TRUE or FALSE

FAPI Example

  $form['description'] = array(
    '#required' => '1',
    '#description' => t('Max. 500 Characters'),
    '#type' => 'textarea',
    '#maxlength' => 500,
    '#maxlength_js' => TRUE,
    '#attributes' =>array(
      'maxlength_js_label' => '@remaining characters left.',
    ),
    '#title' => t('Detailed Description (required)'),
  );
Clone this wiki locally