Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 15 of 15
  1. #1
    Banned
    Join Date
    Dec 2010
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question how i can make a word limit in textarea

    hello, i want to make a word limit on textarea in the below form any body help me- thank you.
    please reply me by editing below code

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>freebacklinkcreator.blogspot.com submit form</title>
    </head>
    <body>
    
    <form method="post" action="sendeail.php">
    
    <!-- DO NOT change ANY of the php sections -->
    <?php
    $ipi = getenv("REMOTE_ADDR");
    $httprefi = getenv ("HTTP_REFERER");
    $httpagenti = getenv ("HTTP_USER_AGENT");
    ?>
    
    <input type="hidden" name="ip" value="<?php echo $ipi ?>" />
    <input type="hidden" name="httpref" value="<?php echo $httprefi ?>" />
    <input type="hidden" name="httpagent" value="<?php echo $httpagenti ?>" />
    
    
    Your Name: <br />
    <select name="visitor" size="1">
    <option value=" Business ">Business </option>
    <option value=" Finance ">Finance </option>
    <option value=" Reference ">Reference </option>
    <option value=" Shopping ">Shopping </option>
    <option value=" Arts and Entertainment ">Arts and Entertainment </option>
    <option value=" Computers ">Computers </option>
    <option value=" Health ">Health </option>
    <option value=" News and Media ">News and Media </option>
    <option value=" Regional ">Regional </option>
    <option value=" Society ">Society </option>
    <option value=" Education ">Education </option>
    <option value=" Internet ">Internet </option>
    <option value=" Recreation ">Recreation </option>
    <option value=" Science and Technology ">Science and Technology </option>
    <option value=" Sports ">Sports </option>
    <option value=" Jobs ">Jobs </option>
    <option value=" Online ">Online </option>
    <option value=" Money ">Money </option>
    <option value=" Affiliate Programs ">Affiliate Programs </option>
    </select>
    <br />
    Your Email:<br />
    <input type="text" name="visitormail" size="35" />
    <br /> <br />
    <br />
    Site Title:<br />
    <input type="text" name="attn" size="35" />
    <br /><br />
    Site Description:
    <br />
    <textarea name="notes" "rows="4" cols="40"></textarea>
    <br />
    <input type="submit" value="Send Mail" />
    <br />
    </form>
    
    </body>
    </html>

  • #2
    Regular Coder Krupski's Avatar
    Join Date
    Dec 2010
    Location
    United States of America
    Posts
    505
    Thanks
    39
    Thanked 47 Times in 46 Posts
    Quote Originally Posted by rakibtg View Post
    hello, i want to make a word limit on textarea in the below form any body help me- thank you.
    please reply me by editing below code
    OK, give this a try... it doesn't count WORDS, it counts characters since words can be of any length. If you want to actually count words, you can probably modify this code to count spaces... on the assumption that there is one space between each word. The added code is highlighted for your convenience. If you want other than a limit of 100, just change all of the "100" to whatever you want (4 places).

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>freebacklinkcreator.blogspot.com submit form</title>
    
    <script type="text/javascript">
    function charCount(text, limit)
    {
        var e = document.getElementById('remain');
        var t = document.getElementById('notes');
        var count = limit - text.length;
        if (count < 1)
        {
            t.value = t.value.substring(0, (limit-1));
        }
        e.innerHTML = 'Characters remaining: ' + count;
    }
    </script>
    
    </head>
    <body>
    
    <form method="post" action="sendeail.php">
    
    <!-- DO NOT change ANY of the php sections -->
    <?php
    $ipi = getenv("REMOTE_ADDR");
    $httprefi = getenv ("HTTP_REFERER");
    $httpagenti = getenv ("HTTP_USER_AGENT");
    ?>
    
    <input type="hidden" name="ip" value="<?php echo $ipi ?>" />
    <input type="hidden" name="httpref" value="<?php echo $httprefi ?>" />
    <input type="hidden" name="httpagent" value="<?php echo $httpagenti ?>" />
    
    
    Your Name: <br />
    <select name="visitor" size="1">
    <option value=" Business ">Business </option>
    <option value=" Finance ">Finance </option>
    <option value=" Reference ">Reference </option>
    <option value=" Shopping ">Shopping </option>
    <option value=" Arts and Entertainment ">Arts and Entertainment </option>
    <option value=" Computers ">Computers </option>
    <option value=" Health ">Health </option>
    <option value=" News and Media ">News and Media </option>
    <option value=" Regional ">Regional </option>
    <option value=" Society ">Society </option>
    <option value=" Education ">Education </option>
    <option value=" Internet ">Internet </option>
    <option value=" Recreation ">Recreation </option>
    <option value=" Science and Technology ">Science and Technology </option>
    <option value=" Sports ">Sports </option>
    <option value=" Jobs ">Jobs </option>
    <option value=" Online ">Online </option>
    <option value=" Money ">Money </option>
    <option value=" Affiliate Programs ">Affiliate Programs </option>
    </select>
    <br />
    Your Email:<br />
    <input type="text" name="visitormail" size="35" />
    <br /> <br />
    <br />
    Site Title:<br />
    <input type="text" name="attn" size="35" />
    <br /><br />
    Site Description (100 chars max):
    <br />
    <textarea id="notes" name="notes" "rows="4" cols="40" onKeyDown="charCount(this.value, 100);" onKeyUp="charCount(this.value, 100);"></textarea>
    <br />
    <span id="remain">Characters remaining: 100</span>
    <br />
    <br />
    <input type="submit" value="Send Mail" />
    <br />
    </form>
    
    </body>
    </html>
    Hope this helps...

    -- Roger

    P.S. Is this a typo:<form method="post" action="sendeail.php">

    Maybe supposed to be "sendemail.php"?
    Last edited by Krupski; 12-22-2010 at 04:44 PM. Reason: added P.S.

  • Users who have thanked Krupski for this post:

    rakibtg (12-23-2010)

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,928
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    http://javascript.internet.com/forms...s-entered.html

    I agree that it is better to limit the number of characters rather than the number of words as words can be of any length and-may-be-run-together-like-this-to-beat-the-limit.

    This counts and includes newlines as well as characters.

    Code:
    <script type = "text/javascript">
    
    // works in IE and FF
    
    function countChar(ID){
    var txt = ID.value;
    var limit = 100;
    nChar = ID.value.match(/[^\r\n]/g);
    nCRLF = ID.value.match(/\n/g);
    if (nChar != null){nChar = nChar.length}
    else {nChar = 0}
    if (nCRLF != null){nCRLF = nCRLF.length}
    else {nCRLF = 0}
    nChar = nChar + nCRLF;
    if (nChar > limit) {
    alert ("You have reached the limit of 400 characters");
    txt = txt.substr(0,limit);
    ID.value = txt;
    }
    }
    
    </script>
    </head>
    
    <body>
    <center>
    <textarea cols=32 rows=10 id='area1'  onkeyup ="countChar(area1)"></textarea>
    </center>
    <br>
    Quizmaster: What season is said to start on the longest day in December?
    Contestant: Spring.
    Quizmaster: December, for God's sake!
    Contestant: Summer.

  • #4
    Regular Coder Krupski's Avatar
    Join Date
    Dec 2010
    Location
    United States of America
    Posts
    505
    Thanks
    39
    Thanked 47 Times in 46 Posts
    Quote Originally Posted by Philip M View Post
    This counts and includes newlines as well as characters.
    Honestly, I think counting newlines is too picky. A person could pre-write the text (say in Notepad or nano) and then paste it into the textarea and squeeze a few extra characters in (since the '\n' are not being counted), but in most cases that's not necessary.

    A limit is to maintain control over post size and long winded writers. If someone ends up squeezing in 108 characters in a 100 character limit by using \n, does it really matter?

    And, if it DOES matter, then I would say the code receiving the text data is flawed (for example, a buffer expecting 100 characters should be declared with more... say 256 characters, "just in case").

    A programmer should plan for the worst case. In this instance, the worst case would probably be a \r\n after each character which could theoretically allow 300 characters posted when the limit is 100. Therefore, the buffer or database cell should be sized for the worst case. IMHO of course...

    -- Roger

  • #5
    Regular Coder
    Join Date
    Aug 2010
    Posts
    967
    Thanks
    19
    Thanked 211 Times in 209 Posts
    Speaking of pasted text, with either solution you still may paste
    more text than the "limit"

  • #6
    Regular Coder Krupski's Avatar
    Join Date
    Dec 2010
    Location
    United States of America
    Posts
    505
    Thanks
    39
    Thanked 47 Times in 46 Posts
    Quote Originally Posted by DaveyErwin View Post
    Speaking of pasted text, with either solution you still may paste
    more text than the "limit"
    You can paset it, but the first keyup or keydown event (in the textarea) will catch it.

    Adding an onclick to the submit button might help... and the PHP that the data is being posted to can also check the size and reject it if it's too big.

    Computers should have a code activated "slapping hand" attached to the monitor.....

    Code:
        if (count > limit)
        {
            alert('I said no more than ' + limit + ' characters! Can\'t you read???');
            slap();
        }
    LOL!!

  • Users who have thanked Krupski for this post:

    TinyScript (12-22-2010)

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,928
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by Krupski View Post
    A programmer should plan for the worst case. In this instance, the worst case would probably be a \r\n after each character which could theoretically allow 300 characters posted when the limit is 100. Therefore, the buffer or database cell should be sized for the worst case. IMHO of course...

    -- Roger
    Yes, that is another approach. But for whatever reason the OP wants to limit the text to some value.

    A simplest count is:-

    Code:
    <textarea id = "ta1" rows = "10" cols = "50" onkeyup = "count()">Some Stuff Typed In Here</textarea>
    
    <script type = "text/javascript">
    function count() {
    var max = 100;
    var txt = document.getElementById("ta1").value;
    var len = txt.length;
    if (len > max) {
    alert ("You have reach the limit of " + max + " characters");
    document.getElementById("ta1").value = document.getElementById("ta1").value.substring(0,max)
    return false;
    }
    }
    </script>
    That includes spaces, punctuation and newlines. It may well suffice in many cases.

    The big weakness of all these word/character limiting scripts is that the user may paste text in, and then key in no more characters. You can (try to) overcome that by:-

    Code:
    <textarea name = "txt" id = "txt" rows = "20" cols = "75"></textarea>
    
    <script type = "text/javascript">
    
    var txtlngth = 0;
    function check4paste() {
    var thetext = document.getElementById("txt").value;
    var newlen = thetext.length;
    if (newlen > (txtlngth + 50)) {
    alert ("You appear to have pasted a substantial amount of text into the textarea.  This is not allowed!");
    document.getElementById("txt").value = thetext.substr(0,txtlngth);  // return to previous text
    }
    else {txtlngth = newlen}
    
    var tim = window.setTimeout("check4paste()", 5000);  // check every 5 seconds
    }
    
    check4paste();
    
    </script>
    And/or, of course, count the number of characters on submit.
    Last edited by Philip M; 12-22-2010 at 05:45 PM.

  • #8
    New Coder
    Join Date
    Oct 2010
    Posts
    29
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by Krupski View Post
    A programmer should plan for the worst case. In this instance, the worst case would probably be a \r\n after each character which could theoretically allow 300 characters posted when the limit is 100. Therefore, the buffer or database cell should be sized for the worst case. IMHO of course...
    Yes, this is true. But wouldn't it be better to limit it to the size of everything so you don't have more data than is needed/ wanted? Obviously JavaScript is not the way to really limit data sizes, as I can disable JavaScript any time I want, but still. Why would you ever allow more than you want? IMHO, there really doesn't need to be a margin of error if you carefully validate everything-- which you should always do.

    I'm still learning, so this is all my opinion and counts for next to nothing, lol.
    Last edited by jmace; 12-22-2010 at 06:52 PM. Reason: Wow. All kinds of typos. . . .

  • #9
    Banned
    Join Date
    Dec 2010
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    woo thank you everybody for such a help this script will be hosted in my free backlink creator blog where you mostly welcome. it is unpublished but it will be publish after some days.

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,211
    Thanks
    75
    Thanked 4,343 Times in 4,309 Posts
    Personally, I see no reason at all to muck with keyup/keydown. Just do it all via a timer.

    Code:
    <script type="text/javascript">
    function limit( )
    {
        var ta = document.forms[0].notes;
        var words = ta.value.replace(/\n/g," \n").split( /[ ]/g );
        if ( words.length > 100 ) 
        {
            ta.value = words.slice( 0, 100).join(" ");
        }
    }
    setInterval( limit, 250 ); // do this every quarter of a second
    </script>
    Presto. Done. It puts a space in front of every newline so that the split and join on space will work. It's not perfect, as it would count a sequence of 5 spaces as 4 words. All that can be fixed easily, if it's important.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #11
    Regular Coder Krupski's Avatar
    Join Date
    Dec 2010
    Location
    United States of America
    Posts
    505
    Thanks
    39
    Thanked 47 Times in 46 Posts
    Quote Originally Posted by rakibtg View Post
    woo thank you everybody for such a help this script will be hosted in my free backlink creator blog where you mostly welcome. it is unpublished but it will be publish after some days.
    Please forgive me for asking, but are you a native English speaker?

    The grammar and spelling on your site is... how can I put it... not good.

    Your front page screams (either) "Foreigner who can't write proper English" or "Teen kid who failed English class a few times".

    Please don't take this the wrong way... hopefully you will see it as constructive criticism...

    -- Roger

    (edit to add): I'll be happy to give you a "clean native English" re-write of your text if you need it.
    Last edited by Krupski; 12-22-2010 at 07:14 PM.

  • #12
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,928
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by Old Pedant View Post
    Personally, I see no reason at all to muck with keyup/keydown. Just do it all via a timer.

    Code:
    <script type="text/javascript">
    function limit( )
    {
        var ta = document.forms[0].notes;
        var words = ta.value.replace(/\n/g," \n").split( /[ ]/g );
        if ( words.length > 100 ) 
        {
            ta.value = words.slice( 0, 100).join(" ");
        }
    }
    setInterval( limit, 250 ); // do this every quarter of a second
    </script>
    Presto. Done. It puts a space in front of every newline so that the split and join on space will work. It's not perfect, as it would count a sequence of 5 spaces as 4 words. All that can be fixed easily, if it's important.
    Is not the weakness with this is that the script is running all the time, and consuming resources, whether text is being entered or not? Presumably you mean
    setTimeout(limit,250); // do this every quarter of a second

  • #13
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,211
    Thanks
    75
    Thanked 4,343 Times in 4,309 Posts
    No, I meant setInterval.

    Sure it's running all the time. So what. Granted, I wouldn't use this on a page where there are tons of objects being moved around (say a video game), but for a simple <form> entry page?? Why not. The client computer will never notice this tiny amount of over head. I doubt that it eats up 100 microseconds every 250 milliseconds. Even if it's 500 microseconds, it's only eating up 2% of the CPU time.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #14
    Regular Coder Krupski's Avatar
    Join Date
    Dec 2010
    Location
    United States of America
    Posts
    505
    Thanks
    39
    Thanked 47 Times in 46 Posts
    Quote Originally Posted by Old Pedant View Post
    No, I meant setInterval.

    Sure it's running all the time. So what. Granted, I wouldn't use this on a page where there are tons of objects being moved around (say a video game), but for a simple <form> entry page?? Why not. The client computer will never notice this tiny amount of over head. I doubt that it eats up 100 microseconds every 250 milliseconds. Even if it's 500 microseconds, it's only eating up 2% of the CPU time.
    Honestly, my first thought was "watch it with a timer" but then I worried about wasting CPU cycles and instead used the keyup/keydown approach.

    Most of the programming I do is on a small (68HC11) microcontroller running at 2 MHz... so every CPU cycle is important to me.

  • #15
    Regular Coder
    Join Date
    Mar 2009
    Location
    Portland Oregon
    Posts
    690
    Thanks
    44
    Thanked 63 Times in 62 Posts
    slapping hand! I LOL'd


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •