Textarea Limit characters per line Jquery or Javascript [duplicate]

穿精又带淫゛_ 提交于 2019-11-28 04:25:44

问题


Possible Duplicate:
How to limit number of characters per line in text area to a fixed value.

hello Friends,

I have a textarea field in my view.

I need to set per line 72 characters length.

that is user is entering more than 72 chracter per line I need go to next line.

How to set these limits using jquery or javascript?

Thanks


回答1:


This is not standard, but it works in many browsers, test it.

http://www.htmlcodetutorial.com/forms/_TEXTAREA_WRAP.html

<TEXTAREA NAME="HARD" COLS="72" ROWS="5" WRAP="HARD">

Setting wrap to hard makes it send new lines to the server. Setting it to soft only breaks it visually.




回答2:


I may be a bit late posting this answer, but this is best done with javaScript to ensure browser compatibility. With jQuery we can do

var count= 1;
var chars= 3;
$('#mytext').keydown(function() {
        var v = $(this).val();
        var vl = v.replace(/(\r\n|\n|\r)/gm,"").length;   
    if (parseInt(vl/count) == chars)
    {
        $(this).val(v + '\n');
        count++;
    }
});

Check working example at http://jsfiddle.net/ZWVad/2/




回答3:


No need to use javascript for this. There is a HTML attribute built into the <textarea> tag.

See some documentation here.

example for your use

<TEXTAREA NAME="HARD" COLS=72 ROWS=5 WRAP=HARD></TEXTAREA>

Using a HARD Wrap actually sets carriage returns when the line is wrapped.




回答4:


If you put this in your html:

<TEXTAREA ID="12" WRAP=HARD></TEXTAREA>
<TEXTAREA ID="92" WRAP=HARD></TEXTAREA>

You can do this with jQuery:

$("#12").attr({cols: 12});
$("#92").attr({cols: 92});


来源:https://stackoverflow.com/questions/5236333/textarea-limit-characters-per-line-jquery-or-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!