开发者

Textarea Limit characters per line Jquery or Javascript [duplicate]

开发者 https://www.devze.com 2023-02-16 01:41 出处:网络
This question already has answers here: Closed 11 years ago. 开发者_如何学Python Possible Duplicate:
This question already has answers here: Closed 11 years ago. 开发者_如何学Python

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


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.


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.


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/

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号