5
Answers

when enter text in textbox. that text count is display in label?

Photo of ajay raju

ajay raju

15y
12.6k
1

hi! sir
i place one textbox and label. i take textboxbox properties is multiline:True and Maxlength:150
and bottom of the text i take lable. and label text is 150. (Characters Left: 150)
my aim is when i enter text in text box , in the label value is decreased. and cut,paste,delete operations are also performed.
means if i cut the text that time label value is increase and if i paste the text label value is decrease.
if label value is reach '0' if i  try to enter text in textbox, text is not entered.
plrase give a code for this.(This is web application)
one doubt i set maxlength is 150 to the textbox control and take multiline is true. if i reach 150 characters and type text also it is print.
why print the text in textbox i reach the maxlength?
please give a solution.
Thanks.   

Answers (5)

1
Photo of Vasanth
NA 1.7k 441.8k 15y
Please add  these attributes in textbox,
  • onkeypress
  • onkeyup
  • onkeydown
  • onPaste

 
asp:textbox id="txtValue" runat="server" onkeypress="return CheckLength(this,150)" onkeyup="return CheckLength(this,150)" onkeydown="return CheckLength(this,150)" onPaste="return CheckLength(this,150)"></asp:textbox>
 
 
[ Javascript code ] 

function CheckLength(txt, maxLen) {
    try {
        if (txt != null) {
            var iLength = txt.value.length
            if (iLength <= maxLen) //Check the Limit.
            {
                //Display the remaining characters
                document.getElementById('<displayControlid>').innerHTML = maxLen - iLength + " are remaining characters.";
            }
            else {
                txt.value = txt.value.substring(0, maxLen);
                return false;
            }
        }
    }
    catch (e) {
        return false;
    }
}
 
 
 
1
Photo of ajay raju
1.3k 398 0 15y

hi amit, how r u. long long time u give reply to my post.
if we not using java script we not give a maxium characters limit? and i am using maxlenght property of textbox. but after characters reach max limit then also we enter values into textbox it takes the characters. why?
Thanks
1
Photo of Jaish Mathews
129 14.4k 2.2m 15y

Hi,
Below one for me. As I understood your need, you don't need to display the total text count, but you need to display remaining possible text count in side TextBox.
My aspx
<asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <p>
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
        <asp:TextBox ID="TextBox1" runat="server" MaxLength="150" Height="207px"
            TextMode="MultiLine"></asp:TextBox>
        <label id="myLabel" style="color:Red;font-weight:bolder">150</label>
    </p>
   
My Codebehind
if (!IsPostBack)
            {
                TextBox1.Attributes.Add("onpropertychange", "handleChange(this);");
            }
My Javascript
function handleChange(ctrl)
{
    var length = ctrl.value.length;
    var displayValue = new Number(150) - new Number(length)
    $get("myLabel").innerText = displayValue.toString();
   
}
1
Photo of Kirtan Patel
NA 21.9k 4.1m 15y
<head runat="server">
<title></title>

<script language="javascript" type="text/javascript">
function Count() {

var v1 = document.getElementById('TextBox1');
var v2 = document.getElementById('lblCount');
v2.innerHTML = v1.value.length;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server" OnKeyPress="Count()"></asp:TextBox>
<asp:Label ID="lblCount" runat="server" Text=""></asp:Label>
</div>
</form>
</body>
</html>
1
Photo of Amit Choudhary
NA 23.4k 5.5m 15y
hi friend,

you can limit your text boxes to the 150 characters using javascript..

<SCRIPT LANGUAGE="JavaScript">
<!-- Web Site: The JavaScript Source -->
<!-- Use one function for multiple text areas on a page -->
<!-- Limit the number of characters per textarea -->
<!-- Begin
function textCounter(field,cntfield,maxlimit) {
if (field.value.length > maxlimit) // if too long...trim it!
field.value = field.value.substring(0, maxlimit);
// otherwise, update 'characters left' counter
else
cntfield.value = maxlimit - field.value.length;
}
// End -->
</script>


<form name="myForm"
action="/articles/articles/javascript/dynamictextareacounter.asp?ID=<%=siteID%>"
method="post">
<b>One Function to Count and Limit Multiple Form Text Areas</b><br>
<textarea name="message1" wrap="physical" cols="28" rows="5"
onKeyDown="textCounter(document.myForm.message1,document.myForm.remLen1,125)"
onKeyUp="textCounter(document.myForm.message1,document.myForm.remLen1,125)"></textarea>
<br>
<input readonly type="text" name="remLen1" size="3" maxlength="3" value="125">
characters left
<br>
<textarea name="message2" wrap="physical" cols="28" rows="5"
onKeyDown="textCounter(document.myForm.message2,document.myForm.remLen2,125)"
onKeyUp="textCounter(document.myForm.message2,document.myForm.remLen2,125)"></textarea>
<br>
<input readonly type="text" name="remLen2" size="3" maxlength="3" value="125">
characters left
<br>
<input type="Submit" name="Submit" value="Submit">
<br>
</form>

See Demo

Please mark as answer if it helps.