Hey you can use following code to accomplish this. It is good idea to tell the user how many characters they have remaining when you use Character counter. The following code is especially useful for textarea fields . Just try to understand each step.
Code:
<script language = "Javascript">
maxLw=500;
var bName1 = navigator.appName;
function taLimit(taObj) {
if (taObj.value.length==maxLw) return false;
return true;
}
function taCount(taObjs,Cnt) {
objCnts=createObject(Cnt);
objVals=taObjs.value;
if (objVals.length>maxLw) objVal=objVal.substring(0,maxLw);
if (objCnts) {
if(bName1 == "Netscape"){
objCnt.textContent=maxLw-objVals.length;}
else{objCnt.innerText=maxLw-objVals.length;}
}
return true;
}
function createObject(objIds) {
if (document.getElementById) return document.getElementById(objIds);
else if (document.layers) return eval("document." + objIds);
else if (document.all) return eval("document.all." + objIds);
else return eval("document." + objIds);
}
</script>
Bookmarks