textarea限制行数和字符限制 [英] textarea with limited lines and char limits

查看:416
本文介绍了textarea限制行数和字符限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要功能,它将TextArea与

  1)最大总行数 -  6和
2)in每行必须有最多16个字符
3)如果用户输入第17个字符,则光标应该到达下一行
并且用户将在那里键入(该行将被计数)
4 )如果用户到达第7行,它将不允许用户写
5)如果用户输入例如你好,我爱StackOverflow及其功能(从第1个字符'H'计算
,第16个char是't',但它是整个单词'StackOverflow',
它不应该中断并继续到下一行,例如
您好,我爱圣
ackOverflow
现在整个字应该来到下一行:

您好,我爱
StackOverflow
及其功能

这里是我到目前为止所做的链接
http ://jsfiddle.net/nqjQ2/2/



有时一些功能性的工作,有时没有,和面向浏览器的问题进行的onkeyup和onkeydown事件
谁能帮助我呢?


解决方案我认为这主要是你想要的:

 < textarea id =splitLines><< ; / textarea的> 

JavaScript:

  var textarea = document.getElementById(splitLines); 
textarea.onkeyup = function(){
var lines = textarea.value.split(\\\
); (行[i] .length <= 16)的
继续;(b)
var j = 0;空间= 16;
while(j ++< = 16){
if(lines [i] .charAt(j)===)space = j;
}
lines [i + 1] = lines [i] .substring(space + 1)+(lines [i + 1] ||);
lines [i] = lines [i] .substring(0,space);
}
textarea.value = lines.slice(0,6).join(\\\
);
};

请参阅小提琴在行动


i need functionaliy which will have TextArea with

1) maximum total lines- 6 and 
2) in each line there must be maximum of 16 chars
3) if user enters 17th character the cursor should go to the next line 
and user will type in there (the line will be counted)
4) if user reaches to the 7th line it will not allow user to write
5) if user type e.g "Hello, I Love StackOverflow and its features" (counting 
from 1st Char 'H', the 16th char is 't' but it is whole word 'StackOverflow',
    it shouldn't break and continue to next line e.g.
        Hello, I Love St
        ackOverflow
now the whole word should come to next line like:

        Hello, I Love
        StackOverflow 
        and its features

here is the link what i have done so far http://jsfiddle.net/nqjQ2/2/

sometimes some of the functionality work, some times not, and facing browser issues for onKeyUp and onKeyDown can anyone help me with it ?

解决方案

I think this is mostly what you want:

<textarea id="splitLines"></textarea>

JavaScript:

var textarea = document.getElementById("splitLines");
textarea.onkeyup = function() {
    var lines = textarea.value.split("\n");
    for (var i = 0; i < lines.length; i++) {
        if (lines[i].length <= 16) continue;
        var j = 0; space = 16;
        while (j++ <= 16) {
            if (lines[i].charAt(j) === " ") space = j;
        }
        lines[i + 1] = lines[i].substring(space + 1) + (lines[i + 1] || "");
        lines[i] = lines[i].substring(0, space);
    }
    textarea.value = lines.slice(0, 6).join("\n");
};

See the fiddle in action.

这篇关于textarea限制行数和字符限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆