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
»

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

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 Use jQuery to Set Textarea Max Length

If you have a textarea field on any of your sites, it might be a good ...

getTable
»

15 Awesome jQuery Table Plugins

Tables are the most preferred way to display data because rows and ...

article image
»

How to Dynamically Open External Links in New Window

Any web developer will probably know that using the ...

article image
»

How to Use jQuery’s .not() Method

jQuery’s .not() method is a method that allows you to return ...

article image
»

jQuery Snippets: How to Add/Remove Classes After Delay

In your projects, there can often be instances of wanting to change ...