String Length function in JavaScript

Length of a string can be calculated by using JavaScript length function. Here is an example of how to get length of a string.

var my_str="Hello world!"

The above string will give a out put of 12. Note that the command will count back and forward blank space also.

Counting data entered by user ( example )

We ask user to enter data in a text area or text box. For our data validation or other requirements we can check the length of the data entered by the user. Here is an example if it.

We will display a text box and when user clicks the button the onclick event will trigger a function. Inside the function we will read the data entered by user and will display the number of characters.
<script type="text/javascript">
function check_text1_length(){
var t1=document.getElementById("text1").value;
document.getElementById("msg").innerHTML="The lenght of data = " + t1.length;

<input type=text name=text1 id=text1><input type=button onclick=check_text1_length(); value=Check>
<div id=msg></div>

Subscribe to our YouTube Channel here


* indicates required
Subscribe to plus2net

    Nibedita Sahu


    How to remove blank spaces between two characters using javascript

    Post your comments , suggestion , error , requirements etc here .

    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2023 All rights reserved worldwide Privacy Policy Disclaimer