Lets Have a fun with Technology.


Follow by Email

Monday, March 23, 2009

Count Number of Lines in TextArea (Textbox) With Word Wrap

Count Number of Lines in TextArea (Textbox) With Word Wrap
Java Script Function to Count Number of Lines with Word Wrap:-
< script type="text/javascript" >
function countLines(){
var theLines1 = document.getElementById("<%= txt_detail.ClientID %>");
var theLines = theLines1.value.replace((new RegExp(".{"+theLines1.cols+"}","g")),"\n").split("\n");
if(theLines[theLines.length-1]=="") theLines.length--;
document.getElementById("<%= txt_line.ClientID %>").value = theLines.length;
< /script >
Call This Java Script Function from where you Want count number Of Lines in TextArea.I am Display on Load of Control So I called its Before Content Tag Finished.
< script >
< /script >
Count Number of Word and Character in Text Area
Used Following Extra Code in Your Java Script Function for Count Number of Character and Words.

var char_count = theLines1.value.length;
var fullStr = theLines1.value + " ";
var initial_whitespace_rExp = /^[^A-Za-z0-9]+/gi;
var left_trimmedStr = fullStr.replace(initial_whitespace_rExp, "");
var non_alphanumerics_rExp = rExp = /[^A-Za-z0-9]+/gi;
var cleanedStr = left_trimmedStr.replace(non_alphanumerics_rExp, " ");
var splitString = cleanedStr.split(" ");
var word_count = splitString.length -1;
if (fullStr.length <2) {
word_count = 0;}
document.getElementById("<%= txt_word.ClientID %>").value =
document.getElementById("<%= txt_char.ClientID %>").value =