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

»

Get Content Marketing Into Your System

The days of marketers hoisting up a brand campaign once every few ...

»

Combine HTML5 & Bootstrap As Key Ingredients For Rebooting Your Business

HTML5 has achieved the tremendous heights in the recent years owing ...

»

10 Tips On Social Media That Will Land Your Dream Job

A job search trends survey conducted by Jobvite showed that 48% of ...

»

10 Best Node.js Frameworks For Web And Apps Development

Node.js is counted among one of the most renowned JavaScript runtime ...

»

Fetch Your Customers : Retail Commerce

As listening the word commerce it comes in our mind that it is ...

»

Best App Design Practices for 2016

The year 2015 witnessed some groundbreaking revolution regarding app ...

»

9 Websites To Visit For Exemplary Footer Design Inspiration

When we set out to design a website, our primary focus is on the ...

»

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