Limit number of lines in textarea and Display line count using jQuery

Viewed 66132

Using jQuery I would like to:

  • Limit the number of lines a user can enter in a textarea to a set number
  • Have a line counter appear that updates number of lines as lines are entered
  • Return key or \n would count as line
$(document).ready(function(){
  $('#countMe').keydown(function(event) {
    // If number of lines is > X (specified by me) return false
    // Count number of lines/update as user enters them turn red if over limit.

  });   
});


<form class="lineCount">
  <textarea id="countMe" cols="30" rows="5"></textarea><br>
  <input type="submit" value="Test Me">
</form>

<div class="theCount">Lines used = X (updates as lines entered)<div>

For this example lets say limit the number of lines allowed to 10.

5 Answers

Here is little improved code. In previous example you could paste text with more lines that you want.

HTML

<textarea data-max="10"></textarea>
<div class="theCount">Lines used: <span id="linesUsed">0</span></div>

JS

jQuery('document').on('keyup change', 'textarea', function(e){

        var maxLines = jQuery(this).attr('data-max');        
        newLines = $(this).val().split("\n").length;

        console.log($(this).val().split("\n"));

        if(newLines >= maxLines) {
            lines = $(this).val().split("\n").slice(0, maxLines);
            var newValue = lines.join("\n");
            $(this).val(newValue);
            $("#linesUsed").html(newLines);
            return false;
        }

    });

For React functional component that sets new value into state and forwards it also to props:

const { onTextChanged,  numberOfLines, maxLength } = props;
const textAreaOnChange = useCallback((newValue) => {
        let text = newValue;
        if (maxLength && text.length > maxLength) return
        if (numberOfLines) text = text.split('\n').slice(0, numberOfLines ?? undefined)
        setTextAreaValue(text); onTextChanged(text)
    }, [numberOfLines, maxLength])

For the React fans out there, and possibly inspiration for a vanilla JS event handler:

onChange={({ target: { value } }) => {
    const returnChar = /\n/gi
    const a = value.match(returnChar)
    const b = title.match(returnChar)
    if (value.length > 80 || (a && b && a.length > 1 && b.length === 1)) return
    dispatch(setState('title', value))
}}

This example limits a textarea to 2 lines or 80 characters total.

It prevents updating the state with a new value, preventing React from adding that value to the textarea.

Related