最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Count carriage returns in javascript - Stack Overflow

programmeradmin1浏览0评论

I'm running the following code to set a limit on characters in a textarea:

(function($){ 
 $.fn.extend({  
     limit: function(limit,element) {

        var interval, f;
        var self = $(this);

        $(this).focus(function(){
            interval = window.setInterval(substring,100);
        });

        $(this).blur(function(){
            clearInterval(interval);
            substring();
        });

        substringFunction = "function substring(){ var val = $(self).val();var length = val.length + carriages;if(length > limit){$(self).val($(self).val().substring(0,limit));}";
        if(typeof element != 'undefined')
            substringFunction += "if($(element).html() != limit-length){$(element).html((limit-length<=0)?'0':limit-length);}"

        substringFunction += "}";

        eval(substringFunction);



        substring();

    } 
}); 
})(jQuery);

The area my users are entering text into can fit a maximum of 1500 characters. The problem is that it can only fit a maximum of 10 lines. If they enter 25 characters on one line before entering a carriage return, the characters total needs to be at 150, not 26.

How can I acplish this?

I'm running the following code to set a limit on characters in a textarea:

(function($){ 
 $.fn.extend({  
     limit: function(limit,element) {

        var interval, f;
        var self = $(this);

        $(this).focus(function(){
            interval = window.setInterval(substring,100);
        });

        $(this).blur(function(){
            clearInterval(interval);
            substring();
        });

        substringFunction = "function substring(){ var val = $(self).val();var length = val.length + carriages;if(length > limit){$(self).val($(self).val().substring(0,limit));}";
        if(typeof element != 'undefined')
            substringFunction += "if($(element).html() != limit-length){$(element).html((limit-length<=0)?'0':limit-length);}"

        substringFunction += "}";

        eval(substringFunction);



        substring();

    } 
}); 
})(jQuery);

The area my users are entering text into can fit a maximum of 1500 characters. The problem is that it can only fit a maximum of 10 lines. If they enter 25 characters on one line before entering a carriage return, the characters total needs to be at 150, not 26.

How can I acplish this?

Share Improve this question asked Jul 28, 2013 at 22:04 DabellatorDabellator 771 silver badge10 bronze badges 3
  • Why are you using eval? Please don't us eval. – Alex Wayne Commented Jul 28, 2013 at 22:21
  • Thanks for the heads up! I hired someone to build the site for me, now I'm going through and changing things. Do you have a link to a different snippet I should use? This level of javascript is over my head. – Dabellator Commented Jul 28, 2013 at 22:47
  • It's over your head because it's way over engineered... It's using advanced meta-programming features where plain simple code would have done just fine. Be careful who you hire to write JavaScript for you. They may not be doing you any favors. – Alex Wayne Commented Jul 28, 2013 at 23:08
Add a ment  | 

2 Answers 2

Reset to default 7
var string = "foo\nbar\nbaz";
console.log(string);
// foo
// bar
// baz

var lines = string.split(/\n/).length;

Simply split the string by every new lines, and then see how many lines you have.

Another solution using regex that is always very efficient. Note that this solution counts retuen carriages, NOT lines

const string = "foo\nbar\nbaz";
const lines = (string.match(/\n/g) || []).length;
console.log(lines);

发布评论

评论列表(0)

  1. 暂无评论