Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

jQuery Limit Number of Characters in Textarea (Textbox) Even Copy Paste Text

Jul 30, 2014
Introduction:

here I will explain how to use
jQuery to limit number of characters in textarea(textbox) even copy and paste text or  jQuery restrict textbox(textarea) length even copy and paste text in textbox with example.

Description:

In previous post I explained
jQuery Tag cloud example in asp.net, jQuery show datatable on aspx page in asp.net, send html page as mail body in asp.net, how to send mail with attachment in asp.net and many more articles related to jQuery,asp.net, c# and vb.net. Now I will explain how to how to restrict textbox(textarea) length even copy and paste text in textbox with example in jQuery

To implement this we need to write the code lilke as shown below


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>jQuery Limit Number of Characters in a TextArea</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script type='text/javascript'>
$(function() {
$('#textarea1').keyup(function() {
var desc = $('#textarea1').val();
var len = desc.length;
if (desc.length >= 150) {
this.value = this.value.substring(0, 150);
}
$('#spntxt').text(150 - len + ' Characters Left');
});
});
</script>
</head>
<body>
<textarea id="textarea1" cols="20" rows="8"></textarea><br/>
<span id="spntxt"> 150 Characters left</span>
</body>
</html>
Live Demo

To check live demo try to copy and paste text in textarea it will restrict to 150 characters

Enter Text:

150 Characters left


If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

1 comments :

Anonymous said...

If I Enter the character in text area wide out of range 150, It allows the character. but only shows the message for (0 Characters Left )

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.