<!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>jQuery Limit Characters in Textarea</title> <script type='text/javascript' src='http://code.jquery.com/jquery-1.8.3.js'></script> <style type='text/css'> body { font-family:Calibri; } .remaining { color:blue; } textarea { font-family:Calibri; } </style> <script type='text/javascript'>//<![CDATA[ $(window).load(function(){ $(function () { var nMaxLength = 150; $("#txtDesc").keydown(function (event) { LimitCharacters($(this)); }); $("#txtDesc").keyup(function (event) { LimitCharacters($(this)); }); function LimitCharacters(txtDesc) { if (txtDesc.val().length > nMaxLength) { txtDesc.val(txtDesc.val().substring(0, nMaxLength)); } else { var nRemaining = nMaxLength - txtDesc.val().length; $('.remaining').text(nRemaining); } } }); });//]]> </script> </head> <body> <textarea name="txtDesc" rows="4" cols="50" id="txtDesc" onDrop="return false;" style="width:70%;"></textarea> <br/>Remaining: <b><span class="remaining">150</span></b> </body> </html>
article
Tuesday, July 21, 2015
How to Limit Number of Characters in Textarea using jQuery
How to Limit Number of Characters in Textarea using jQuery