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 8 of 8
  1. #1
    CBG
    CBG is offline
    New Coder
    Join Date
    Feb 2010
    Location
    UK
    Posts
    91
    Thanks
    26
    Thanked 0 Times in 0 Posts

    Question Count on returns (gaps) Text counter - Help

    Hi,

    I have the below code it works, but there a problem.
    If you press return to drop a line it counts that as a letter/number.

    Is there a way to fix it?

    <html>
    <head>
    <title></title>
    </head>
    <body>
    <script language="JavaScript">
    function CheckCount()
    {
    var maxChar = 2000000;
    if(document.frmMain.txtDetail.value.length > maxChar)
    {
    return false;
    }
    else
    {
    document.frmMain.txtCount.value = document.frmMain.txtDetail.value.length;
    }
    }
    </script>
    <body>
    <form name="frmMain" method="post">
    <textarea name="txtDetail" cols="40" rows="5" id="txtDetail" onKeyUp="return CheckCount()"></textarea>
    <br>
    <input name="txtCount" type="text" id="txtCount" style="width:65px;" value="0">
    </form>
    </body>
    </html>
    Last edited by CBG; 01-11-2012 at 10:05 PM.
    Regards,
    CBG

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,775
    Thanks
    55
    Thanked 519 Times in 516 Posts
    how about a little workaround?

    Code:
    <html>
    <head>
    <title></title>
    </head>
    <body>
    <script type="text/javascript">
    var enters=0;
    function CheckCount(e){
    if (window.event) {keycode = window.event.keyCode;}
    else if (e) {keycode = e.which;}
    var maxChar = 2000000;
    if(document.frmMain.txtCount.value > maxChar){
    return false;
    }
    else {
    if (keycode == 13)   {
    enters++
    }
    document.frmMain.txtCount.value = document.frmMain.txtDetail.value.length-enters;
    	}
    }
    
    
    </script>
    <body>
    <form name="frmMain" method="post">
    <textarea name="txtDetail" cols="40" rows="5" id="txtDetail" onKeyUp="return CheckCount(event)"></textarea>
    <br>
    <input name="txtCount" type="text" id="txtCount" style="width:65px;" value="0">
    </form>
    </body>
    </html>
    Last edited by xelawho; 01-11-2012 at 06:24 PM.

  • Users who have thanked xelawho for this post:

    CBG (01-11-2012)

  • #3
    CBG
    CBG is offline
    New Coder
    Join Date
    Feb 2010
    Location
    UK
    Posts
    91
    Thanks
    26
    Thanked 0 Times in 0 Posts
    Just tested this in ie9 and it has the same problem, each return is counted.
    Regards,
    CBG

  • #4
    New Coder
    Join Date
    Jan 2012
    Location
    Columbus, Ohio, U.S.A
    Posts
    41
    Thanks
    0
    Thanked 8 Times in 8 Posts
    hi there, this worked for me... (not fully tested though)

    I'd hate to loop through it constantly, there's got to be a more efficient way
    somebody might be able to do this through regular expressions or something


    anyway, here it is:

    function CheckCount()
    {

    var maxChar = 2000000;

    var result = ''; //a string to hold the text without special chars
    for(var i=0; i<document.frmMain.txtDetail.value.length; i++){

    //loop through and find carriage returns or newline characters
    if(document.frmMain.txtDetail.value.charAt(i)=='\n' ||
    document.frmMain.txtDetail.value.charAt(i)== '\r')
    {
    //we find one, get outta here!
    continue;

    }else{
    //otherwise append it to our result string
    result += document.frmMain.txtDetail.value.charAt(i);
    }

    }



    if(result.length > maxChar)
    {
    return false;
    }
    else
    {
    document.frmMain.txtCount.value = result.length;
    }
    }

  • Users who have thanked adaminaudio for this post:

    CBG (01-11-2012)

  • #5
    CBG
    CBG is offline
    New Coder
    Join Date
    Feb 2010
    Location
    UK
    Posts
    91
    Thanks
    26
    Thanked 0 Times in 0 Posts
    Thanks adaminaudio that works for me
    Regards,
    CBG

  • #6
    New Coder
    Join Date
    Jan 2012
    Location
    Columbus, Ohio, U.S.A
    Posts
    41
    Thanks
    0
    Thanked 8 Times in 8 Posts
    hey, You're very welcome

  • #7
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,775
    Thanks
    55
    Thanked 519 Times in 516 Posts
    Quote Originally Posted by CBG View Post
    Just tested this in ie9 and it has the same problem, each return is counted.
    oh, yeah... I just noticed that IE counts every "return" as two characters

    never a dull moment

    adaminaudio's solution is much more practical - here's the regex version, in case you're interested:

    Code:
    function CheckCount() {
    var maxChar = 2000000;
    if(document.frmMain.txtCount.value > maxChar){
    return false;
    }
    else{
    result=document.frmMain.txtDetail.value.replace(/\n|\r/g,"")
    document.frmMain.txtCount.value = result.length;
    	}
    }

  • #8
    New Coder
    Join Date
    Jan 2012
    Location
    Columbus, Ohio, U.S.A
    Posts
    41
    Thanks
    0
    Thanked 8 Times in 8 Posts
    great thanks for that regEx solution xelawho, I'll put that in my box!

    I just started on regular expressions and I'm still trying to get my head around the syntax....


    -Adam


  •  

    Posting Permissions

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