The MU forums have moved to WordPress.org

How to limit the number / amount of characters used in a comments form (1 post)

  1. Farms2
    Member
    Posted 17 years ago #

    So, you wanna limit the number of characters in a comment form... possibly to *encourage* people to trackback/pingback rather than leave mamouth comments... well, here's one way of doing it:

    In each theme you want this to happen in pop this is the header:

    <script type="text/JavaScript">
    function textCounter(field,counter,maxlimit,linecounter) {
    // text width//
    var fieldWidth = parseInt(field.offsetWidth);
    var charcnt = field.value.length;
    var comment = 0; //fix Set the initial value of the textarea id comment
    // trim the extra text
    if (charcnt > maxlimit) {
    field.value= field.value.substring(0, maxlimit);
    }
    else {
    // progress bar percentage
    var percentage = parseInt(100 - (( maxlimit - charcnt) * 100)/maxlimit) ;
    document.getElementById(counter).style.width = "200px";
    document.getElementById(counter).innerHTML="Available space used: "+percentage+"%"
    // color correction on style from CCFFF -> CC0000
    setcolor(document.getElementById(counter),percentage,"background-color");
    }
    }
    function setcolor(obj,percentage,prop){
    obj.style[prop] = "rgb(80%,"+(100-percentage)+"%,"+(100-percentage)+"%)";
    }
    function CheckLength()
    {
    if (document.getElementById("comment").value.length < 0 ) {
    document.getElementById("submit").disabled=true;
    }
    else document.getElementById("submit").disabled=false;
    }
    </script>

    Then, in your comments.php replace the regular textarea for commnts with this:

    <textarea onKeyDown="textCounter(this,'progressbar1',500)"
    onKeyUp="textCounter(this,'progressbar1',500), CheckLength()"
    onFocus="textCounter(this,'progressbar1',500)"
    name="comment" id="comment" cols="80%" rows="10">
    </textarea>
    <div id="progressbar1" class="progress" >
    <script>textCounter(document.getElementById("commentsform"),"progressbar1",9000)</script></div>
    <input name="submit" type="submit" id="submit" tabindex="5" value="Submit Comment" />
    <input type="hidden" name="comment_post_ID" value="<?php echo $id; ?>" />
    <?php if (function_exists('live_preview')) { live_preview(); } ?>
    <?php @include('constants.php'); echo $commentsFormInclude; ?>
    </div>

    My implementation of this code and it seems to work very nicely yes :)

About this Topic

  • Started 17 years ago by Farms2