Skip to content

Instantly share code, notes, and snippets.

@MadhuSubedi
Created December 15, 2016 04:12
Show Gist options
  • Select an option

  • Save MadhuSubedi/b0c758cb898aac7e7899552c4da24fc1 to your computer and use it in GitHub Desktop.

Select an option

Save MadhuSubedi/b0c758cb898aac7e7899552c4da24fc1 to your computer and use it in GitHub Desktop.
Limitation For Textarea
$(document).ready(function () {
$('textarea[data-limit-rows=true]').on('keypress', function (event) {
var textarea = $(this),
numberOfLines = (textarea.val().match(/\n/g) || []).length + 1,
maxRows = parseInt(textarea.attr('rows'));
maxLength = textarea.data('max-charperline');
if (event.which === 13 && numberOfLines === maxRows ) {
return false;
}
var text = $(this).val();
var lines = text.split(/(\r\n|\n|\r)/gm);
for (var i = 0; i < lines.length; i++) {
if (lines[i].length > maxLength) {
lines[i] = lines[i].substring(0, maxLength);
}
}
$(this).val(lines.join(''));
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment