How To: Limit Input Box Characters To Defined Count Using jQuery


Earlier today working on a project , I found one trick which I would like to share. Here is a small code that limit a input box for defined number of characters.

Please take a look on following example:

HTML

1
<input id="myInput" value=""/>

.

Jquery snippet

Assuming we have to limit characters to 20.

1
2
3
4
5
6
7
$(document).ready(function(){	
	$('#myInput').keyup( function() {
		var $this = $(this);
		if($this.val().length > 20)
			$this.val($this.val().substr(0, 20));			
	});
     });

.

That’s it, yeah its done!! You can check Working Demo

Any updations, suggestions and corrections are welcome.

Recently Published

»

What are the Cloud Computing trends to dominate in 2016

With Cloud computing, it has developed cell phones into a remote ...

»

How to Show Related Posts in WordPress Without a Plugin?

Making your visitors go through each post increases the browsing time ...

»

Noteworthy Facts About Android App Piracy and How It Can Be Dealt With

Are you a gaming aficionado, and obsessed with gaming apps? Do you ...

»

4 Great Ways to Turn Your Images into SEO Tools

Photos on a website help us to understand the website much more ...

»

8 Search Form Tutorials Built With CSS3, jQuery & HTML5

Elevate the power of search by adding search functionality or search ...

»

10 Best HTML5 Frameworks For Hybrid App Development

Hybrid apps are generally built around native web view container or ...

»

A Look at The Evolution Of WordPress and What Future Holds For It

When it comes to developing a website, WordPress is the first name ...

»

7 WordPress Plugins That Make Your Site Mobile Responsive

Not much in the recent past, having a responsive website that works ...

»

Things you should Consider When You Are Designing an E-commerce Website

The people of today’s generation have discovered the various ...