Check TextArea Max Length using Javascript


If u have a textbox and your requirement is to find the length of the textbox & limit the user to enter the number of charecters then do the following .

Write the below javascript code :

<script type="text/javascript">
function ismaxlength(objTxtCtrl,nLength)
{
  if (objTxtCtrl.getAttribute && objTxtCtrl.value.length > nLength)
     objTxtCtrl.value = objTxtCtrl.value.substring(0, nLength);
 if(document.all)
    document.getElementById('lblCaption').innerText=objTxtCtrl.value.length +' Out Of '+nLength;
 else
    document.getElementById('lblCaption').textContent=objTxtCtrl.value.length +' Out Of '+nLength;
}
</script>

Then drag & drop lable & textbox control in your page.

<asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine" onkeyup="return ismaxlength(this,125)"></asp:TextBox>
<asp:Label ID="lblCaption" style="font-family:Tahoma;font-size:1em;font-weight:bold" runat="server" Text=""></asp:Label>

Comments