validating textarea using javascript/dhtml

March 4, 2008

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. .

2 Comments Add your own

  • 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,

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


Spam Blocked

Flickr Photos

THE-GROUP-YOU-CANNOT-GET-INTO-ANYMORE

.pouting.

L'informarsi, prima di tutto

More Photos

Categories

Links

Calendar

March 2008
M T W T F S S
« Feb   Apr »
 12
3456789
10111213141516
17181920212223
24252627282930
31  

Recent Posts

Recent Comments

Sachin Gajraj on Top 50 Govt Engineering C…
Sachin Gajraj on Top 50 Govt Engineering C…
KUSHAGRA on Top 50 Govt Engineering C…
Siber on 5 useful PHP functions for MyS…
Daniel Tome on Applying background to Iframe …

Pages

Top Clicks

Top Posts

Blog Stats