Set maximum lengthe for textbox using JavaScript with remaining characters

Posted by Neeks under ASP.NET category on | Views : 7733
Please write this script in your ASPX page header part
<script language="javascript" type="text/javascript">
Set maximum lengthe for textbox using JavaScript with remaining characters
This function is used to validate the textbox for the Maximum lengh
txtBox --> This is the parameter to be passed as a textbox object
maxLength --> This parameter is used to pass the Maximum length of the Textbox
We have to call this function for onkeyup and onkeydown events of the textbox or textArea
function fnClicked(txtBox, maxLength)
//Here we are validating the Length of the Text.
//If the text is greater than the provided lenth, we have to ignore the last characters
if(txtBox.value.length >= maxLength)
//Removing the last characters which are more than the length
txtBox.value = txtBox.value.substring(0, 160);
//Displaying the Remaining characters
document.getElementById('lbl').innerHTML = maxLength - txtBox.value.length;

This is the code for your form or dic tag

Here we are creating Label and TextArea
Label -> will be used to display the remaining text in the TextArea
TextArea -> is used to take the input from the user to validate.

. Your HTML Code
Character Left: <asp:Label ID="lbl" runat="server" Text="160"></asp:Label><br />
<%--Here, we are calling the function fnClick(...) with the help of onkeyup and onkeydown events--%>
<asp:TextBox runat="server" ID="txt" TextMode="MultiLine" onkeyup="fnClicked(this, 160)" onkeydown="fnClicked(this, 160)">

. Your HTML Code

Comments or Responses

Login to post response