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 Use Google Analytics with jQuery Mobile

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

article image
»

jQuery Snippets: Using jQuery to Check All Boxes

When you’ve given your users a ton of┬ácheckbox input fields ...

article image
»

Useful jQuery Code Snippets for Working with Images

Images are an essential part of any website and sometimes working ...

article image
»

How to Use jQuery to Check if Cookies are Enabled

A cookie (not the kind with chocolate chips) is a small piece of data ...

article image
»

A Beginner’s Guide to jQuery’s Selectors

We talk a lot about event methods on this site, but not so much about ...

article image
»

Add Keyboard Shortcut to Anchor Tag Using jQuery

Making a user friendly website can be quite a challenging task. There ...

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