Friday

How to limit the number of characters in the Summary textarea field

Version: Drupal 7
  
You need to restrict the length of Article summary to avoid a very long one.
One way is to use this script:

<script>
function limitText(limitField, limitNum) {
    if (limitField.value.length > limitNum) {
        limitField.value = limitField.value.substring(0, limitNum);
    } 
}
</script>
<form >
<textarea rows="5" cols="30" onKeyDown="limitText(this,20);" 
onKeyUp="limitText(this,20);">
</textarea>
</form>
 
The script limit the number of characters, and check the length while writing not after submitting the Article.
 
Test it and try to enter more than 20 characters:





Now the important step: is Where to add this?

Open the file:
modules/field/modules/text/text.module


find the following code:

'#description' => t('Leave blank to use trimmed value of full text as the summary.'),
        '#attached' => array(
          'js' => array(drupal_get_path('module', 'text') . '/text.js'),
        ),
        '#attributes' => array('class' => array('text-summary')),
        '#prefix' => '<div class="text-summary-wrapper">',
        '#suffix' => '</div>',
        '#weight' => -10,
      );
      // Fall through to the next case.

1.Adding the two events
onKeyDown="limitText(this,20);" 
onKeyUp="limitText(this,20);"

update the following sentence
   '#attributes' => array('class' => array('text-summary')),
 to be

  '#attributes' => array('class' => array('text-summary'), 'onKeyDown' =>array('limitText(this,200);'), 'onKeyUp'=> array('limitText(this,200);')),

change 200 to the number of characters you want.


2.Adding the script
we'll add it before the code of the summary textarea.

Update:
        '#prefix' => '<div class="text-summary-wrapper">',
to be

        '#prefix' => '<script>
function limitText(limitField, limitNum) {
    if (limitField.value.length > limitNum) {
        limitField.value = limitField.value.substring(0, limitNum);
    }
}
</script><div class="text-summary-wrapper">',


Enjoy ....