www.webdeveloper.com
Results 1 to 6 of 6

Thread: Character count minus spaces

  1. #1
    Join Date
    Jul 2006
    Posts
    7

    Character count minus spaces

    I need this character count to NOT include spaces typed in each of the 4 fields.

    Can you help?

    Thanks!

    MY CODE:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Untitled Document</title>
    <script type="text/javascript">
    function countChars()
    {
    document.form1.count_display.value = (document.form1.text_1.value.length + document.form1.text_2.value.length + document.form1.text_3.value.length + document.form1.text_4.value.length);
    }

    </script>
    </head>
    <body>
    <form action="" name="form1" id="form1">
    <p><input name="text_1" onkeyup="countChars();" onblur="countChars();" /></p>
    <p><input name="text_2" onkeyup="countChars();" onblur="countChars();" /></p>
    <p><input name="text_3" onkeyup="countChars();" onblur="countChars();" /></p>
    <p><input name="text_4" onkeyup="countChars();" onblur="countChars();" /></p>
    <br />
    <p>Char Count: <input type="text" name="count_display" value="0" size="4" readonly="readonly" /></p>
    </form>
    </body>
    </html>

  2. #2
    Join Date
    Feb 2003
    Location
    Michigan, USA
    Posts
    5,774
    First, combine all three fields into one string, store this as one variable.

    Create a second variable, and make it equal to the first.

    Replace all occurrences of " " in the second variable with an empty string "".

    Subtract the length of the first string from the length of the second string.

  3. #3
    Join Date
    Jul 2006
    Posts
    7
    I'm new to Javascript, so I'm unclear on how to do all that.

  4. #4
    Join Date
    Feb 2003
    Location
    Michigan, USA
    Posts
    5,774

  5. #5
    Join Date
    May 2007
    Posts
    129
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Couunter</title>
    <script type="text/javascript">
    function countChars()
    {
    var theForm = document.form1,
    	allValues = theForm.text_1.value + theForm.text_2.value + theForm.text_3.value + theForm.text_4.value,
    	allValuesNoSpaces = allValues.replace(/\s+/g,'');
    
    document.form1.count_display.value = allValuesNoSpaces.length;
    }
    
    </script>
    </head>
    <body>
    <form action="" name="form1" id="form1">
    <p><input name="text_1" onkeyup="countChars();" onblur="countChars();" /></p>
    <p><input name="text_2" onkeyup="countChars();" onblur="countChars();" /></p>
    <p><input name="text_3" onkeyup="countChars();" onblur="countChars();" /></p>
    <p><input name="text_4" onkeyup="countChars();" onblur="countChars();" /></p>
    <br />
    <p>Char Count: <input type="text" name="count_display" value="0" size="4" readonly="readonly" /></p>
    
    </form>
    </body>
    </html>

  6. #6
    Join Date
    Dec 2008
    Posts
    5

    Smile

    Code:
    element = document.getElementById(id);
    this will get you the element as an object in the JS, then...

    Code:
    var string = element.value;
    will get you the value of the element inside a var named straing, then...

    Code:
    string = string.replace(/ /g, "");
    will take your string, remove all the white spaces and replace them with a no-space, then...

    Code:
    var length = string.length;
    will get you the length of your new string...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles