I want to limit number of chars in input text field.
Code I\'m using:
function limitText(field, maxChar){
if ($(field).val().length > maxChar)
you can also do it like this:
<input type="text" name="usrname" maxlength="10" />
to achieve this with jQuery, you can do this:
function limitText(field, maxChar){
$(field).attr('maxlength',maxChar);
}
Your code is working in FF. Here is slightly modified version of your code:
$('input.testinput').on('keyup', function() {
limitText(this, 10)
});
function limitText(field, maxChar){
var ref = $(field),
val = ref.val();
if ( val.length >= maxChar ){
ref.val(function() {
console.log(val.substr(0, maxChar))
return val.substr(0, maxChar);
});
}
}
Demo