asp.net限制文本框字数并显示剩余字数

以下代码是直接从vs2005开发环境下复制过来的.可以直接复制过去运行.
这个程序在处理汉字的时候还存在一点问题.就是限定了120个字符,60个汉字,但有可能就只能输入55个,很奇怪,正在找原因.以后会更新过来.如有高手看到,也请帮忙改改...谢谢.....

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
    <script type="text/javascript">
    function fucCheckLength(strTemp)
     {
     var i,sum;
     sum=0;
     for(i=0;i<strTemp.length;i++)
  {
   if ((strTemp.charCodeAt(i)>=0) && (strTemp.charCodeAt(i)<=255))
    sum=sum+1;
   else
    sum=sum+2;
  }
  return sum;
 }
        function ShowLeft(){
 var LenString,LenStringI,Strings;
 LenString=fucCheckLength(document.getElementById("TextBox1").value);
 LenStringI=LenString;
 if (LenString>120){
  alert("输入的字符长度不能超过120(汉字60个)!");
  Strings=document.getElementById("TextBox1").value;
  while(LenStringI>120){
   if ((Strings.charCodeAt(Strings.length)>=0) && (Strings.charCodeAt(Strings.length)<=255)){
    LenStringI=LenStringI-1;
   }
   else{
    LenStringI=LenStringI-2;
   }
   Strings=Strings.substring(0,(Strings.length-1));
  }
  document.getElementById("TextBox1").value=Strings;
  return false;
 }
 document.getElementById("Button1").value=250-LenString;
 
}
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server" Height="156px" TextMode="MultiLine" Width="326px" onKeyUp="return ShowLeft();"></asp:TextBox>
        <input id="Button1" type="button" value="120" />
        </div>
    </form>
</body>
</html>

转载于:https://www.cnblogs.com/cnyyl/archive/2007/07/25/831072.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值