Created
December 15, 2016 04:12
-
-
Save MadhuSubedi/b0c758cb898aac7e7899552c4da24fc1 to your computer and use it in GitHub Desktop.
Limitation For Textarea
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $(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