jQuery way to limit input characters using javascript substring function

It’s an example of useful jQuery way to limit user entering a maximum set number of character in your input fields. Example makes use of javascript substring function to limit user upto 20 character which is triggered on keyup event. It truncate anything extra to that limit.

$("input[name=Title]").keyup(function(){
		var text = $(this).val();
		if(text.length > 20) {
			text = text.substring(0, 20);
			$(this).val(text);
		}
	})

Leave a Reply

Your email address will not be published. Required fields are marked *