Friday 13 September 2013

How to calculate text length of Textarea in Jquery




<script type="text/javascript" src="js/jquery-1.8.0.js"></script>
<script type="text/javascript">
$(document).ready(function(){

var TotalLength = 50;

$("#txt-imgdisc").keyup(function(evt){
  var charCode = (evt.which) ? evt.which : event.keyCode;

var textlength = $(this).val().length;
if(textlength > TotalLength)
{
if(charCode === 8)
{
return true;
}
return false;
}
$("#spresult").text(TotalLength-textlength);

});
$("#txt-imgdisc").keydown(function(evt){
  var charCode = (evt.which) ? evt.which : event.keyCode;
var textlength = $(this).val().length;
if(textlength > TotalLength)
{
if(charCode === 8)
{
return true;
}
return false;
}
  $("#spresult").text(TotalLength-textlength);
  });
});
 </script>
<p style="color:red; font-weight:bold;">Enter Text Less then 50</p> <textarea id="txt-imgdisc" name="imgdisc" rows="5" cols="30" class="required require" maxlength="400" validate="Please enter the image discription."></textarea>
 <span id ="spresult" style="color:green; font-weight:bold;" >50</span>


No comments:

Post a Comment