views:

29

answers:

2

I need to enforce character count limit in my application. I found some JavaScripts from web but they are not working, Here is my code. I would be very thankful if someone can kindly see the code & correct whats causing problem.

<HTML>
<HEAD>
<script type="text/javascript" src="http://ajax.googleapis.com/ 
ajax/libs/jquery/1.4.2/jquery.min.js">
</script> 
<script type="text/javascript"> 
$(document).ready(function() 
{ $("#update").keyup(function()
{ 
var box=$(this).val(); 
var main = box.length *100; 
var value= (main / 140); 
var count= 140 - box.length; 
if(box.length <= 140) 
{ 
$('#count').html(count); 
}
else 
{ 
alert('Character Limit Exceeded!'); 
return false; 
} 
});}); 
</script> 
</HEAD>
<BODY>
<textarea id="update" rows="10" cols="3"></textarea>
<div align="left" id="character-count">
<div id="count">140</div> 
</div>
</BODY>
</HTML>
+1  A: 

You have a space in between http://ajax.googleapis.com/ and ajax

methodin
thanX a Lot methodin, you fabulos people :-)
XCeptable
Wanna hit the checkbox next to my suggestion to accept it? ;)
methodin
A: 

Your code will not handle someone keeping a key pressed (as it will not fire keyup events until you release the key). It will also not handle pasting text with more chars than the allowed number..

Have a look at this plugin http://remysharp.com/2008/06/30/maxlength-plugin/ which handles most cases..

Gaby