145

How can I get the length of text entered in a textbox using jQuery?

2
  • @Izzy That's all right, since this question is old enough to bump up in google results. Thereby I present you one of the most well known help vampires of SO.
    – Qwerty
    Jun 6, 2016 at 12:18
  • And herein lies support for an answer I gave on meta.stackoverflow, which received 39 downvotes before it was deleted by the stack overflow status quo, where I argued that even low quality novice questions accrete value. I am vindicated. They didn't like my answer because it grated against the egos of the high-rep incumbent members who think StackOverflow is all about them.
    – TARKUS
    Jun 15, 2016 at 16:55

6 Answers 6

333
var myLength = $("#myTextbox").val().length;
0
108

If your textbox has an id attribute of "mytextbox", then you can get the length like this:

var myLength = $("#mytextbox").val().length;
  • $("#mytextbox") finds the textbox by its id.
  • .val() gets the value of the input element entered by the user, which is a string.
  • .length gets the number of characters in the string.
2
  • 27
    Up Vote because you explained what it all does, not jsut provided the code.
    – user83632
    May 2, 2009 at 16:53
  • If your text field doesn't have an id, you can reference it by name. Do a $("input[name='myinputname']").val().length;
    – TARKUS
    Jun 15, 2016 at 17:46
10

For me its not text box its a span tag and this worked for me.

var len = $("span").text().length;
5

Below mentioned code works perfectly fine for taking length of any characters entered in textbox.

$("#Texboxid").val().length;

2

CODE

$('#montant-total-prevu').on("change", function() {

var taille = $('#montant-total-prevu').val().length;

    if (taille > 9) {

//TODO

}

});
1

You need to only grab the element with an appropriate jQuery selector and then the .val() method to get the string contained in the input textbox and then call the .length on that string.

$('input:text').val().length

However, be warned that if the selector matches multiple inputs, .val() will only return the value of the first textbox. You can also change the selector to get a more specific element but keep the :text to ensure it's an input textbox.

On another note, to get the length of a string contained in another, non-input element, you can use the .text() function to get the string and then use .length on that string to find its length.

2
  • 2
    -1 You're wrong, it only returns the length of the very first input box's value. jsfiddle.net/8Khzf
    – alexia
    Apr 29, 2011 at 21:19
  • Thanks alot. Its helpful. I learn new way to check input type. before seeing your answer i was used to $("input[type='text']").
    – Zohaib
    Jun 7, 2016 at 7:57

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.