I have a few areas on my site where I need to limit text input to X amount of characters and it\'s nice to show a number of spaces left while a user types in, like twitter d
var max = 140;
$("#spn").html(max+" Remaining");
$("#textarea").keyup(function () {
var lenght1 = $("#textarea").val().length;
var remaining = max - lenght1;
$("#spn").html(remaining + " Remaining");
if (remaining < 0) { $("#spn").css('opacity', '0.4').css('color', 'red'); $("#comment").attr('disabled', 'disabled'); } else { $("#spn").css('opacity', '1').css('color', 'black'); $("#comment").removeAttr('disabled'); }
if (remaining < 10) { $("#spn").css('color', 'red'); } else { $("#spn").css('color', 'black'); }
});
I used jQuery and the answer by deceze and then tweaked it to give me a twitter-style countdown so that users could see by how much they had gone over and adjust their text accordingly. I also put it into its own function so I could call it from another function that sometimes populated the textarea automatically:
function reCalcToText() {
var left = 200 - $('.toText').val().length;
if (left < 0) {
$('#counter').text(left);
$('#counter').addClass("excess");
} else {
$('#counter').text(left);
$('#counter').removeClass();
}
}
function onReady() {
$('#<%= toText.ClientID %>').keyup(function() {
reCalcToText();
});
};
I've used Aaron Russell's simply countable jQuery plugin with success
Simple usage:
$('#my_textarea').simplyCountable();
Advanced usage:
$('#my_textarea').simplyCountable({
counter: '#counter',
countable: 'characters',
maxCount: 140,
strictMax: false,
countDirection: 'down',
safeClass: 'safe',
overClass: 'over',
thousandSeparator: ','
});
Sometimes you need to have more than one text area and want them addressed individually without having to code for each. This code dynamically adds the chars remaining to any having a maxLength attribute...
<script type="text/javascript">
$(document).ready(function(){
$('textarea[maxLength]').each(function(){
var tId=$(this).attr("id");
$(this).after('<br><span id="cnt'+tId+'"></span>');
$(this).keyup(function () {
var tId=$(this).attr("id");
var tMax=$(this).attr("maxLength");//case-sensitive, unlike HTML input maxlength
var left = tMax - $(this).val().length;
if (left < 0) left = 0;
$('#cnt'+tId).text('Characters left: ' + left);
}).keyup();
});
});
</script>
<textarea id="myTextarea" maxLength="200">Initial text lorem ipsum blah blah blah</textarea><br><br>
<textarea id="mySecondTextarea" maxLength="500"></textarea><br><br>
<textarea id="textareaWithoutCounter"></textarea>
Very simple in jQuery:
<textarea id="myTextarea"></textarea>
<p id="counter"></p>
<script type="text/javascript">
$('#myTextarea').keyup(function () {
var left = 200 - $(this).val().length;
if (left < 0) {
left = 0;
}
$('#counter').text('Characters left: ' + left);
});
</script>
Substitute the 200 by whatever your limit is.
Note this does not limit the actual text input, it just counts down. You need to check server-side for the input length, this is just a visual helper.
As an aside, I don't think you even should try to limit the input length by denying any input when the limit is reached. It's a pain in the rear usability-wise and can't be relied upon anyway. A simple countdown and server-side checking is the best option IMHO.
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function()
{
$('#TestId').keyup(function(e)
{
var maxLength = 100;
var textlength = this.value.length;
if (textlength >= maxLength)
{
$('#charStatus').html('You cannot write more then ' + maxLength + ' characters!');
this.value = this.value.substring(0, maxLength);
e.preventDefault();
}
else
{
$('#charStatus').html('You have ' + (maxLength - textlength) + ' characters left.');
}
});
});
</script>
</head>
<body>
<textarea id="TestId" cols="20" rows="8"></textarea><br />
(Maximum characters: 100)<br />
<span id="charStatus"></span>
</body>
</html>