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 13 of 13
  1. #1
    New Coder
    Join Date
    Oct 2005
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    javascript textarea count

    Hi I'm looking for ways to limit the textarea input, I dont want people to enter hundreds of lines cause, i only want them to enter max. 10 lines. I've looked at several javascripts (cause the problem only can be solved by javascript), and came up with this script: http://javascript.internet.com/forms...-textarea.html . It works great and even has a counter how many characters are left to type in. However when I copy paste something, the 'counter' doesnt adjust right away...it does when I try to type in something extra though. Is it possible to make the counter adjust automaticly whenever there is pasted ?

    I guess you guys have to test to see what I mean. Thanks in advance !!

  2. #2
    Regular Coder Nischumacher's Avatar
    Join Date
    Oct 2005
    Location
    Bombay, India
    Posts
    196
    Thanks
    0
    Thanked 2 Times in 2 Posts
    HeLL oHH...

    the javascript function is called up on onKeyDown and onKeyUp events...

    call it up on onMouseUp and onMouseDown events as well...

    that should work...
    - NS 666
    .net DEVILoper

  3. #3
    New Coder
    Join Date
    Oct 2005
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i tried, but it doesnt do the job.. when i copy and past text into the textarea I still have to type in an extra character before the 'character-counter' is updated

  4. #4
    Regular Coder Nischumacher's Avatar
    Join Date
    Oct 2005
    Location
    Bombay, India
    Posts
    196
    Thanks
    0
    Thanked 2 Times in 2 Posts
    HeLL Yo... sporter

    i tried callinging the function on onMouseUp and onMouseDown... yup... it doesn't work...

    call them up on onMouseMove and onMouseOut... along with onKeyDown and onKeyUp events...

    tried and tested with positive results... this has to do the job...
    - NS 666
    .net DEVILoper

  5. #5
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,584
    Thanks
    3
    Thanked 516 Times in 503 Posts
    to enter max. 10 lines.
    the script you are using counts charactors
    counts of charactors or words are fine

    difficult to count lines unless sure user uses 'Enter' for a new line

    however it may be possible if you specify a font-size and line length in px

    If you realy want to count lines, I may be able to help

  6. #6
    New Coder
    Join Date
    Oct 2005
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts
    sorry for the confusion... i want to limit the number of characters.

  7. #7
    Regular Coder Nischumacher's Avatar
    Join Date
    Oct 2005
    Location
    Bombay, India
    Posts
    196
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by vwphillips
    however it may be possible if you specify a font-size and line length in px
    sporter wanted to limit the characters... which i hope has been done...

    as you said... it is difficult to count lines...
    but if you can help... i would want to know how to count the number of lines entered in a textarea... although this is not my query... just to tinker in my free time...
    - NS 666
    .net DEVILoper

  8. #8
    New Coder
    Join Date
    Oct 2005
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Count lines

    Actually, it is quit possible to count the lines. and rather easy too. you would use the same as the char counter. only instead of txt.value.length, you would use txt.value.split("\r\n").length.


    this is some ugly code. but i had to do it quick. i'm in class
    Code:
    <script>
    var tempObj = obj
    function checkLines(obj,limit) { 
    
    var s = obj.value.split("\r\n") // split the string into an array based on the enter chars (ascii 13 and 10) linefeed carriage return
    var a = new Array()
    if (s.length > limit) { //if it is > than the limit, crop the the value
    for (i=0;i<limit;i++) { 
    a[i] = s[i]
    }
    obj.value = a.join("\r\n") //join the array back to it's delimiter (crlf)
    tempObj = obj
    obj.disabled = true //disable so that can't input while you warn them
    
    setTimeout("reenable("+limit+")",5) //set a timer to re-enabled the object, also so it doesn't flood them with alerts if they are holding ENTER down
    
    }
    }
    function reenable(n) { 
    if (tempObj) { 
    alert("You have reached the limit of lines which is: " + n)
    tempObj.disabled = false
    tempObj.focus()
    tempObj = null
    
    }
    }
    </script>
    <textarea style="width:400px;height:200px" onkeydown="checkLines(this,10)"></textarea>
    Last edited by fleshth; 10-25-2005 at 07:19 PM. Reason: Insert code,correction

  9. #9
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,584
    Thanks
    3
    Thanked 516 Times in 503 Posts
    fleshth you miss the point

    difficult to count lines unless sure user uses 'Enter' for a new line
    no 'Enter' no \n

  10. #10
    Regular Coder
    Join Date
    Dec 2002
    Posts
    169
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Use a setInterval to periodically recount the number of characters/words/sentences/lines/paragraphs (or whatever unit is important). That'll assure that it gets updated.

  11. #11
    Regular Coder
    Join Date
    Sep 2005
    Location
    Chicago, IL
    Posts
    160
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You can kind of limit the number of lines. If your text area has 30 cols then that's 30 characters for 1 line, so if you want to limit it to 10 lines then you would limit it to 300 characters (number of cols x number of lines you want it limited too).
    I'm only telling you how I would do it, not how its supposed to be done. ;)

  12. #12
    Regular Coder
    Join Date
    Sep 2005
    Location
    Chicago, IL
    Posts
    160
    Thanks
    0
    Thanked 0 Times in 0 Posts
    oh yeah, and if you wanted to count line you could do the same thing. if your text area is 30 cols then just do a character count and divide it by 30. that way if you have 60 chars entered it will divide it by 30 and come up with 2 lines. The only thing is, that if it isn't a complete line (30 chars) then you'll get a decimal like 1.8 lines total or something like that.
    I'm only telling you how I would do it, not how its supposed to be done. ;)

  13. #13
    New to the CF scene
    Join Date
    Apr 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If you are looking for a cleaner code , you can find a live character counter meter here :
    http://youhack.me/2010/04/22/live-ch...-using-jquery/

    This one contains a progress bar as well which increases when you type in characters .


 

Posting Permissions

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