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

4 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?

    Reply
  • 2. Jane Crone  |  May 22, 2008 at 3:14 pm

    Tallahassee,

    Reply
  • 3. sdsd  |  March 10, 2009 at 6:43 am

    kkk

    Reply
  • 4. xx  |  October 27, 2009 at 1:28 pm

    xxx

    Reply

Leave a Comment

Required

Required, hidden

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

Trackback this post  |  Subscribe to the comments via RSS Feed


Spam Blocked

Flickr Photos

27/365 : Escape

Large Format Study N. 14

let the wind blows

More Photos

Blogroll

Recent Posts

Recent Comments

Satheeshmuthu on Sachin silences the critics [ …
Satheeshmuthu on Sachin silences the critics [ …
izdelava spletne str… on Convert em,px,pt and % in…
CSS References, Tuto… on Convert em,px,pt and % in…
shirley on Convert em,px,pt and % in…

Pages

Top Clicks

Top Posts

Blog Stats