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

Screen Shot 2017-04-23 at 10.15.41 AM
»

How to Use jQuery to Create a Sticky Navigation on Scroll

Creating “sticky” elements is something that can often be ...

article image
»

How to Use jQuery’s Keystroke Methods

jQuery has a few built-in keystroke methods that are all triggered by ...

article image
»

How to Change iFrame Styling Using jQuery

If you’ve ever had the displeasure of working with iFrames, ...

article image
»

How to Use jQuery to Create Blinking Text

Blinking text is one of those cool features that you never actively ...

article image
»

Convert Images to Black and White using jQuery

Converting an image to black and white is no longer a manual event. ...

article image
»

How to Disable Right-Click Menu Using jQuery

There can be dozens of reasons why you may want to disable right ...

article image
»

How to Optimize jQuery Code for Better Performance

jQuery is the most popular client side library, in fact almost every ...

article image
»

Using Head.js to Load All Your JavaScript

In the past few years, client side frameworks/libraries have become ...

article image
»

How to Detect Browser Using jQuery

One of the more frustrating parts of creating a new web project is ...