Limit number of characters in input field
Maxlength attribute- for browser, that support this feature.
Javascript - for others.
<input class="test-input" type="text" maxlength="12" />
<script>
$('.test-input').unbind('keyup change input paste').bind('keyup change input paste',function(e){
var $this = $(this);
var val = $this.val();
var valLength = val.length;
var maxCount = $this.attr('maxlength');
if(valLength>maxCount){
$this.val($this.val().substring(0,maxCount));
}
});
</script>
http://jsfiddle.net/tvpRT/
make use of maxlength
in input
tag
<input type="text" maxlength="20" />
This should work for you I think.
HTML
<input type="text" name="myText" id="myText" data-maxlength="10" />
jQuery
$('#myText').keyup(validateMaxLength);
function validateMaxLength()
{
var text = $(this).val();
var maxlength = $(this).data('maxlength');
if(maxlength > 0)
{
$(this).val(text.substr(0, maxlength));
}
}