I wrote small character counter & limiter.
You can test it here: Char Counter & Limiter
jQuery Code:
$(document).ready(function() {
$('.counter').each(function(index) {
$('.counter p span').html('0'); // set character length to 0
$('input', this).keyup(function() {
var CounterDiv = $(this).parent(); //
var Limit = $('p', CounterDiv).html().split(' / ')[1];
var CurrentLength = $(this).val().length;
if (CurrentLength == Limit) {
$('p span', CounterDiv).html(CurrentLength);
return false;
} else if (CurrentLength > Limit) {
var str = $(this).val();
$(this).val(str.substring(0, str.length - 1));
return false;
} else {
$('p span', CounterDiv).html(CurrentLength);
}
});
});
});
Have you got any suggestion how can I improve that? I’m not 100% sure with the syntax.
Use native html
maxlengthattribute for inputshere is the sample: http://jsfiddle.net/8YdCh/16/
html
js