January 5, 2010

Count the Number of Words in a TextArea using jQuery




If you were looking out for a script to count the number of words in a TextArea, then here it is:

<html xmlns="http://www.w3.org/1999/xhtml">
<
head>
<
title>Count Words in a TextArea</title>

<
script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js">
</
script>

<
script type="text/javascript">
$(function() {
$("#btnCount").click(function() {
var strText = jQuery.trim($('#txtArea').val());
alert('Total No of Words are: ' + strText.split(/\s+/).length);
});
});
</script>
</
head>
<
body>
<
div>
<
textarea id="txtArea" cols="20" rows="10">
</
textarea>
<
br />
<
input id="btnCount" type="button" value="Count Words" />
</
div>
</
body>
</
html>

OUTPUT

image

See a Live Demo



'Like' us on our FaceBook page if you find this blog useful. Thanks!


Did you like this post?
kick it on DotNetKicks.com Save on Delicious
subscribe via rss subscribe via e-mail
print this post follow me on twitter


About The Author

Suprotim Agarwal, ASP.NET Architecture MVP works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal

comments

0 Responses to "Count the Number of Words in a TextArea using jQuery"
 

Copyright © 2009-2012 All Rights Reserved for DevCurry.com by Suprotim Agarwal | Terms and Conditions