简体   繁体   English

怎么算不。 在多行文本框中输入的字符,直到140个字符

[英]How to count no. of characters entered in multiline textbox till 140 characters

How to count no. 怎么算不。 of characters entered in multiline textbox, i wanna enter maximum 140 characters in my multiline textbox using vb.net ... 在多行文本框中输入的字符,我想使用vb.net在我的多行文本框中输入最多140个字符...

In short i want textbox to enter limit is only 140 characters .... 总之,我希望文本框输入限制只有140个字符....

i have te following code to do that .... but i wanna implement 140 characters limit in multiline textbox : 我有以下代码来做到这一点....但我想在多行文本框中实现140个字符限制:

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

           document.getElementById("Label1").innerHTML = document.getElementById("TextBox2").value.length;
        }
</script>


<asp:TextBox ID="TextBox2" runat="server" Height="78px" 
            TextMode="MultiLine" Width="224px" onkeyup="Count(this.id)" 
            MaxLength="140"></asp:TextBox>

Since you tagged your question with asp.net I'm assuming you're talking about a web application. 由于您使用asp.net标记了您的问题,我假设您正在谈论Web应用程序。

If it is so, this job is best done by a client side script like JavaScript, and not VB.NET, you should use the latter to do a server side check only after the user has submitted the form, and use the former while he is still entering the data. 如果是这样,这项工作最好由JavaScript等客户端脚本完成,而不是VB.NET,只有在用户提交表单后才应使用后者进行服务器端检查,并在使用前者时使用前者仍在输入数据。

On the internet there are many examples on how to do this, just look for something like " javascript count characters ". 在互联网上有很多关于如何做到这一点的例子,只需寻找像“ javascript count characters ”这样的东西。

Here's an example of how to do this. 这是一个如何做到这一点的例子。 The function takes the number of characters you're limiting to, the (client, HTML) id of the control to count, and the id of the element to output the count into 该函数获取您要限制的字符数,要计数的控件的(客户端,HTML)ID,以及将计数输出到的元素的id

<script type="text/javascript">
    function CountChars(charLimit, inputId, outputId) {
        var remainingChars = charLimit - document.getElementById(inputId).value.length;
        document.getElementById(outputId).innerHTML = remainingChars;
    }   
</script>

And here's a usage example 这是一个用法示例

    <asp:TextBox ID="TextBox2" Width="100%" TextMode="MultiLine" Rows="10" MaxLength="140"
        runat="server"></asp:TextBox>
</p>
<p>
    Characters remaining: <span id="characterCount">140</span></p>

Hook up the event handler in code behind, in the page load event: 在页面加载事件中隐藏代码后面的事件处理程序:

Protected Sub AddCharCountHandler()
    TextBox2.Attributes.Add("onkeyup", "CountChars(" & 140 & ", '" & TextBox2.ClientID & "'," & "'characterCount');")
End Sub

Note that I'm calling the function using txtDescription.ClientId, although my output span has a fixed id. 请注意,我正在使用txtDescription.ClientId调用该函数,尽管我的输出范围具有固定的id。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM