validating textarea using javascript/dhtml
March 4, 2008 at 2:45 am 7 comments
So many times we need to validate textarea to limit users from entering more charachters
Here is the code which is tested in IE for validating textarea for 255 charachters.
/*copy this code in head tag */
<script language = “Javascript”>
/**
* Author : suresh kumar
*/
maxL=255;
function taLimit(taObj) {
if (taObj.value.length==maxL) return false;
return true;
}
function taCount(taObj,Cnt) {
objCnt=createObject(Cnt);
objVal=taObj.value;
if (objVal.length>maxL) objVal=objVal.substring(0,maxL);
if (objCnt) objCnt.innerText=maxL-objVal.length;
return true;
}
function createObject(objId) {
if (document.getElementById) return document.getElementById(objId);
else if (document.layers) return eval(“document.” + objId);
else if (document.all) return eval(“document.all.” + objId);
else return eval(“document.” + objId);
}
</script>
/*copy this in body */
<font> Maximum Number of characters for this text box is 255.<br>
<textarea onKeyPress=”return taLimit(this)” onKeyUp=”return taCount(this,’myCounter’)” name=”Description” rows=7 wrap=”physical” cols=40>
</textarea>
<br><br>
You have <B><SPAN id=myCounter>255</SPAN></B> characters remaining
for your description…</font>
Entry filed under: Javascript.
7 Comments Add your own
Leave a reply to xx Cancel reply
Trackback this post | Subscribe to the comments via RSS Feed
1. Jaffer Haider | March 4, 2008 at 4:12 pm
Hmm … will this handle the case if a user pastes text into the textarea?
2. Jane Crone | May 22, 2008 at 3:14 pm
Tallahassee,
3. sdsd | March 10, 2009 at 6:43 am
kkk
4. xx | October 27, 2009 at 1:28 pm
xxx
5. Anoop | September 29, 2011 at 1:00 pm
Thanks
6. Laura | November 4, 2011 at 6:45 pm
Land na chalat ba ukada baith ke
7. talib | November 17, 2011 at 5:03 am
ddxxzvv vfvvvv