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>

About these ads

Entry filed under: Javascript. Tags: .

Sachin silences the critics [ especially sanjay manjrekar] Ricky Pointing : CB- Series song [ taare zameen par]

7 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
  • 5. Anoop  |  September 29, 2011 at 1:00 pm

    Thanks

    Reply
    • 6. Laura  |  November 4, 2011 at 6:45 pm

      Land na chalat ba ukada baith ke

      Reply
  • 7. talib  |  November 17, 2011 at 5:03 am

    ddxxzvv vfvvvv

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


Flickr Photos

Castillo de Barcience

They Dance with their Gods in Kerala at Bharani time in Kodungaloor

Dervish house, Blagaj.

More Photos

Blog Stats

  • 644,770 hits

Follow

Get every new post delivered to your Inbox.

Join 36 other followers

%d bloggers like this: