TextBox 动态计算输入字符长度
在一个TextBox中,把属性设为 Multiline 后,Maxlength失效,怎么动态计算输入的字符长度?也就是在边输入字符的同时自动计算已输入的字符长度,当输入一定的长度后就会自动提示超出长度范围. 请问怎么实现好?
------解决方案--------------------textBox1.TextChanged += new EventHandler(textBox1_TextChanged);
void textBox1_TextChanged(object sender, EventArgs e)
{
if(this.textBox1.Text.Length > 100)
{
MessageBox.Show( "不能超过100. ");
this.textBox1.Text = this.textBox1.Text.Substring(0, 100);
}
}
------解决方案--------------------http://www.dynamicdrive.com/dynamicindex16/maxlength.htm
------解决方案--------------------function account()
{
var maxLength = 5;
var obj = document.getElementById( "TextBox2 ");
if(obj.value.length > maxLength)
{
obj.value = obj.value.substr(0,maxLength);
alert( "输入超过最大字符 "+maxLength+ " 个 ");
}
}
<asp:TextBox ID= "TextBox2 " runat= "server " TextMode= "MultiLine " onpropertychange= "account() " > </asp:TextBox>
------解决方案--------------------楼上的比较正确~
http://www.dynamicdrive.com/dynamicindex16/maxlength.htm
http://www.cnblogs.com/jhtchina/articles/317466.html
有点小bug!