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!"
document.write(my_str.length)



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;
}
</script>

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

Number of User Comments : 1


Google+

Nibedita Sahu

18-01-2011

How to remove blank spaces between two characters using javascript

Post Comment This is for short comments only. Use the forum for more discussions.






OPINION POLL

How you handle Search Engine Optimization ( SEO ) for your website
HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us
©2000-2015 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer