3

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 accomplish this?

3
  • Why are you using eval? Please don't us eval. 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. 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. Commented Jul 28, 2013 at 23:08

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Not sure if this will do the trick for me. First, I would like the user to be notified of the characters they have used up. If each line is worth 150 characters, the difference needs to be subtracted from the total if they carriage return early. When they reach the maximum, they shouldn't be allowed to enter anything further.
You asked how to count new lines, this is how. How to prevent input based on some function you run on each keystroke is another question.
0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.