I want to prevent user to enter text in textarea once it reaches a max character limit. What was happening that when i reached to max limit then my text-area scroll-bar move
<script type="text/javascript">
$(document).ready(function () {
maxlength("TextArea1");
});
function maxlength(id) {
$('#' + id).on('input propertychange', function () {
CharLimit(this, 20);
});
}
function CharLimit(input, maxChar) {
var len = $(input).val().length;
if (len > maxChar) {
$(input).val($(input).val().substring(0, maxChar));
}
}
</script>
<textarea maxlength="400"> </textarea>
Use the above code to limit the number of characters inserted in a text area, if you want to disable the textarea itself (ie, will not be able to edit afterwards) you can use javascript/jquery to disable it.
The keyup
event fires after the default behaviour (populating text area) has occurred.
It's better to use the keypress
event, and filter non-printable characters.
Demo: http://jsfiddle.net/3uhNP/1/ (with max length 4)
jQuery(document).ready(function($) {
var max = 400;
$('textarea.max').keypress(function(e) {
if (e.which < 0x20) {
// e.which < 0x20, then it's not a printable character
// e.which === 0 - Not a character
return; // Do nothing
}
if (this.value.length == max) {
e.preventDefault();
} else if (this.value.length > max) {
// Maximum exceeded
this.value = this.value.substring(0, max);
}
});
}); //end if ready(fn)
You could directly give maxlength
to textarea
to disable itself. But, you want to showing appropriate message then use keyup
event for default behavior and textarea
length
for calculating charcter and display suitable message.
HTML
<div id="count"></div>
<textarea class="max" maxlength="250" id="tarea"></textarea>
<div id="msg"></div>
jQuery
$(function(){
var max = parseInt($("#tarea").attr("maxlength"));
$("#count").text("Characters left: " + max);
$("#tarea").keyup(function(e){
$("#count").text("Characters left: " + (max - $(this).val().length));
if($(this).val().length==max)
$("#msg").text("Limit Reached....");
else
$("#msg").text("");
});
});
Demo Fiddle
Just write this code in your Javascript file.. But Need to specify the 'maxlength' attribute with textarea. This will work for all textarea of a page.
$('textarea').bind("change keyup input",function() {
var limitNum=$(this).attr("maxlength");
if ($(this).val().length > limitNum) {
$(this).val($(this).val().substring(0, limitNum));
}
});
For those already using ES2015 in your browsers, here's an implementation using some of the answers above:
class InputCharacterCount {
constructor(element, min, max) {
this.element = element;
this.min = min;
this.max = max;
this.appendCharacterCount();
}
appendCharacterCount(){
let charCount = `<small class="char-counter help-block"></small>`;
this.element.closest('.form-group').append(charCount);
}
count(event){
this.element.attr('maxlength', this.max); // Add maxlenght attr to input element
let value = this.element.val();
this.element
.closest('.form-group')
.find('.char-counter')
.html(value.length+'/'+this.max); // Add a character count on keyup/keypress
if (value.length < this.min || value.length > this.max) { // color text on min/max changes
this.element.addClass('text-danger');
} else {
this.element.removeClass('text-danger');
}
}
}
Usage:
let comment = $('[name="collection-state-comment"]');
let counter = new InputCharacterCount(comment, 21, 140);
$(comment).keyup(function(event){
counter.count(event);
});