jcadima
3/31/2017 - 7:46 PM

Limit Number of Characters in input fields

Limit Number of Characters in input fields



http://stackoverflow.com/questions/8669268/limit-number-of-characters-in-input-field

Just HTML:

<input type="text" maxlength="15" />


jQuery for browsers that dont support it
http://jsfiddle.net/tvpRT/

<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>