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 >= 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
»

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 ...

article image
»

Check all checkboxes in HTML table using jQuery

HTML tables are a very handy and useful element. They can adapt to ...

article image
»

Changing images in a loop on hover using jQuery

Why use this Function? Changing images in a loop on mouse over is a ...

article image
»

How to Dynamically Trim Text Using jQuery

This easy beginners jQuery tutorial demonstrates how you can use ...

article image
»

jQuery Snippets: Getting the Current Year

Here’s a fun, simple, and incredibly useful jQuery that you can ...

article image
»

jQuery Snippets: How to Conditionally Enable Submit

One easy way to prevent users from submitting an empty form in a ...

article image
»

How to Use Google Analytics with jQuery Mobile

jQuery Mobile is an HTML5 based web-page framework that’s ...