1

I´ve read that you can create a mini module to limit comments length. I´ts cool, and it´s working, but I need to add to it some JS so it tells you how many characters you have left.

So I´ve found some easy thing to do (via Google):

Put this inside your head tag:

<script language=”javascript”>
function limit(what,chars,counter) {
if (what.value.length > chars) {
what.value=what.value.substr(0,chars);
alert(‘You exceed to ‘ + chars + ‘chars!’);
}
counting = (chars – what.value.length);
c = document.getElementById(counter);
c.innerHTML = counting;
}
</script>

And put this in your body:

<label for=”text”><strong>Text</strong></label> ¦ Chars left: <span id=”count1″>500</span></p>
<textarea name=”[1][3][t]” rows=”10″ cols=”50″ onkeyup=”limit(this,500,’count1′);” onkeydown=”limit(this,500,’count1′);” onblur=”limit(this,500,’count1′);” onfocus=”limit(this,500,’count1′);”></textarea>

The thing is that I should add that inside the textarea of the comment body. As you can see, the code works, but how do I make it work the Drupal way?

I need only to limit comment length.

Thanks for your help! Rosamunda

1 Answer 1

1

Why don't you simply use the Maxlength module that does exactly what you want already pre-packaged?

Otherwise, I would either write a custom module and use drupal_add_js() and hook_form_alter(), or use them in your template.php file without having to write a module.

If you're stumped, and want to know how to do it, why don't you download the Maxlength module and analyse how it is written to clarify the processes involved?

3
  • Hi there, Thanks for your answer, but it won´t work on comments, just on CCK fields...
    – Rosamunda
    Nov 8, 2011 at 21:56
  • Anyway, I have trouble with the second part of the code, whare should I put it to modify the comment form output?
    – Rosamunda
    Nov 11, 2011 at 19:04
  • I would stick it into a custom module or your template.php file. You should be able to use the hook_form_alter - use prefix to add things before the form (labels chars left etc ..), use the attributes like so to add onkeyup, onkeydown functiosn to the field itself.$form['field_name]['#attributes'] = array('onkeyup' => array('jadajada ...')...); }
    – tecjam
    Nov 15, 2011 at 7:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.