简体   繁体   English

限制文本区域中单行的行数和字母数

[英]Limiting number of lines & letters in single line in textarea

I am trying to limit the number of lines in a text area to 20, and number of characters in each line to 15 for IE8 browser. 我正在尝试将文本区域中的行数限制为20,对于IE8浏览器,每行中的字符数限制为15。 I tried the solutions already available on stackoverflow like https://stackoverflow.com/a/11586266/1453499 however all of them works in chrome and other modern browsers not in IE8. 我尝试了像https://stackoverflow.com/a/11586266/1453499这样的stackoverflow上已经可用的解决方案,但是它们都可以在chrome和IE8中无法使用的其他现代浏览器中使用。 Is there a solution compatible with IE8? 是否有与IE8兼容的解决方案?

I used the combination of two answers ( https://stackoverflow.com/a/3373056/1453499 & https://stackoverflow.com/a/11586266/1453499 ) to find the solution to my problem, below is the final solution 我使用两个答案的组合( https://stackoverflow.com/a/3373056/1453499https://stackoverflow.com/a/11586266/1453499 )找到解决问题的方法,以下是最终解决方案

    function getInputSelection(el) {
        var start = 0, normalizedValue, range,
            textInputRange, len, endRange;

        if (typeof el.selectionStart === "number" && typeof el.selectionEnd === "number") {
            start = el.selectionStart;
        } else {
            range = document.selection.createRange();

            if (range && range.parentElement() === el) {
                normalizedValue = el.value.replace(/\r\n/g, "\n");
                len = normalizedValue.length;

                // Create a working TextRange that lives only in the input
                textInputRange = el.createTextRange();
                textInputRange.moveToBookmark(range.getBookmark());

                // Check if the start and end of the selection are at the very end
                // of the input, since moveStart/moveEnd doesn't return what we want
                // in those cases
                endRange = el.createTextRange();
                endRange.collapse(false);

                if (textInputRange.compareEndPoints("StartToEnd", endRange) > -1) {
                    start = len;
                } else {
                    start = -textInputRange.moveStart("character", -len);
                    start += normalizedValue.slice(0, start).split("\n").length - 1;
                }
            }
        }

        return start;
    }

    $(document).ready(function() {
        //Restrict the search 
        var textArea = $('#textarea_id');
        var maxRows = 30;
        var maxChars = 17;
        textArea.keypress(function(e) {
            var text = textArea.val();
            var lines = text.split('\n');
            if (e.keyCode === 13) {
                return lines.length < maxRows;
            } else { //Should check for backspace/del/etc.
                var caret = getInputSelection(textArea.get(0));
                var line = 0;
                var charCount = 0;
                $.each(lines, function(i, e) {
                    charCount += e.length;
                    if (caret <= charCount) {
                        line = i;
                        return false;
                    }
                    //\n count for 1 char;
                    charCount += 1;
                });

                var theLine = lines[line];
                return theLine.length < maxChars;
            }
        });

    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM