X

LATEST #JQUERY NEWS

How to Use jQuery to Set Textarea Max Length


If you have a textarea field on any of your sites, it might be a good idea f0r you to limit the amount of characters that your users can input into it. Doing this can help prevent against spam, and it can also save you from having to read excessively long messages. If you don’t have a lot of time to read user messages, setting a character limit can be a great way of keeping your users’ submissions short and to the point. Controlling the max length of the amount of characters that are allowed in a textarea can be done dozens of different ways using jQuery. What follows is one simple way to achieve this functionality.

First, you’ll need HTML code to create a textarea. It will probably look something like this:

<textarea id="limit-text" rows="10" cols="20">
<span id="txt-length-left">Characters Left:</span>

In the HTML snippet above, we also included a span tag that we’ll use to inform the user how many characters they have left to add to the text area.

Now, your jQuery code should look like this:

$(document).ready(function() {
    var maxLen = 50;
    $('#limit-txt').keypress(function(event) {
        var Length = $("#limit-txt").val().length;
        var AmountLeft = maxLen - Length;
        $('#txt-length-left').html(AmountLeft);
        if (Length & gt; = maxLen) {
            if (event.which != 48) {
                return false;
            }
        }
    });
});

Here, we’ve set the max length to 50 characters (you can set it to anything you like) and we monitor the amount of characters within the textarea using the keypress method. We also calculate the amount left by subtracting the current length from the max length, and use the html method to add that number to our code. This is a very simple and straightforward way to limit the amount of characters that can be input into a textarea. If you’re looking for something more complex and with more flexibility, try adding a plugin to your project that will handle this functionality for you, and give you plenty of more options in terms of customization and function.

Recently Published

article image
»

7 Responsive jQuery Dropdown Menu Plugins

Dropdown Menu Plugins Dropdown menus are mostly used for website ...

article image
»

20 Stylish and Modern jQuery Tooltip Plugins

jQuery Tooltip Plugins Tooltips are a great option for showing extra ...

article image
»

15 Newest jQuery plugins for responsive website

Due to the proliferation of smartphones and tablets, responsive ...

article image
»

How to use Redux with jQuery

State management is one of most integral parts of any software and a ...

article image
»

Fade HTML Table Row on Hover Using jQuery

HTML tables are the ideal choice for displaying data in tabular form. ...

article image
»

Drag and Drop HTML Table Rows Using jQuery

HTML tables are the preferred UI option for displaying data. Sorting, ...

article image
»

Top 5 Best JavaScript Template Engines

Template engines help in the decoupling of HTML and JavaScript code ...

article image
»

3 jQuery/JavaScript Plugins for Push Notifications

Push notifications started from the mobile business but this ...

article image
»

Create Smooth Animation Using jQuery’s “stop()”

Making Quick Work jQuery’s animate() method is a quick way to ...