This mini module was written in reponse to this forum topic. It allows you to set a minimum required length (measured in characters) of the node title, to prevent users from creating posts with very short titles.

In general, this mini module (written for Drupal 4.7.x) illustrates how hook_nodeapi() and hook_form_alter() can be used to perform additional validation steps on the node editing forms of selected content types. This example focuses on the node title, but the approach for validating other elements, such as the node body, is identical.

Be sure to remove the last ?> and save as a text file in your modules directory as longtitle.module.

You can set the number of required characters in admin->settings->longtitle, and you can choose to which content types the validation applies in admin->settings->content types.


function longtitle_help($section) {
  switch ($section) {
    case 'admin/modules#description':
      return t('Allows to set a minimum title length');
  }
}

function longtitle_nodeapi(&$node, $op, $form = NULL, $page = NULL) {
  switch ($op) {
    case 'validate':
      if (strlen(trim($node->title)) < variable_get('node_title_length', 10) && variable_get('longtitle_'. $node->type, 1)) {
        form_set_error('title', t('Your title is too short'));
      }
      break;
  }
}

function longtitle_settings() {
  $form['node_title_length'] = array(
    '#type' => 'select',
    '#title' => t('Minimum title length'),
    '#default_value' => variable_get('node_title_length', 10),
    '#options' => drupal_map_assoc(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30)),
    '#description' => t('Minimum length of node titles (in characters)'),
  );
  return $form;
}

function longtitle_form_alter($form_id, &$form) {
  if (isset($form['type'])) {
    if ($form['type']['#value'] .'_node_settings' == $form_id) {
      $form['workflow']['longtitle_'. $form['type']['#value']] = array(
        '#type' => 'radios',
        '#title' => t('Check title length'),
        '#default_value' => variable_get('longtitle_'. $form['type']['#value'], 1),
        '#options' => array(t('Disabled'), t('Enabled')),
      );
    }
  }
}