41

How can I count the number of characters in a textbox using jQuery?

$("#id").val().length < 3

just counts upto 3 character spaces but not the number of characters.

4
  • $("#id").val().length returns the number of characters in a string. What's the problem? Commented Jan 11, 2011 at 19:08
  • 1
    What does "just counts upto 3 character spaces but not the number of characters" mean? Commented Jan 11, 2011 at 19:10
  • I need to count the number of characters excluding the spaces, a trim function of the sort jquery.trim() ? Commented Jan 11, 2011 at 19:10
  • I am using $("#id").val().length and when I use .html() to print it out it stays as 0. Commented May 4, 2015 at 17:45

3 Answers 3

116

For length including white-space:

$("#id").val().length

For length without white-space:

$("#id").val().replace(/ /g,'').length

For removing only beginning and trailing white-space:

$.trim($("#test").val()).length

For example, the string " t e s t " would evaluate as:

//" t e s t "
$("#id").val(); 

//Example 1
$("#id").val().length; //Returns 9
//Example 2
$("#id").val().replace(/ /g,'').length; //Returns 4
//Example 3
$.trim($("#test").val()).length; //Returns 7

Here is a demo using all of them.

Sign up to request clarification or add additional context in comments.

3 Comments

That regular expression will do what you need.
@RionWilliams, i used your fiddle and tweaked it a bit, but it's not working. it's here. any ideas?
@yak613 It doesn't look like the code that you provided does anything. It will display a count when the page is first loaded, but nothing much after that. If you wanted to display the count as you typed, you could attach an event handler to the keypress, keyup or keydown events similar to this example or if you just wanted to calculate it on the button click, this example should work.
2

Use .length to count number of characters, and $.trim() function to remove spaces, and replace(/ /g,'') to replace multiple spaces with just one. Here is an example:

   var str = "      Hel  lo       ";
   console.log(str.length); 
   console.log($.trim(str).length); 
   console.log(str.replace(/ /g,'').length); 

Output:

20
7
5

Comments

0

Since none of the given answers to this question has worked for me (I ask myself if jQuery has changed in this regard somehow?), I am adding the solution that does work in my case, i.e. using $('#myelement').text().length; to determine the number of characters inside a tag (defined by tag name or id).

var x = $('h1').text().length;
console.log(x);
$('#solution').text(x);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h1>How many characters are in this H1?</h1>
<p>This is just some more text.</p>
<p style="font-size: 1.4em;">Solution: There are <span id="solution"></span> characters in the h1 tag, including spaces</p>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.