JAVA—咖啡館

          ——歡迎訪問rogerfan的博客,常來《JAVA——咖啡館》坐坐,喝杯濃香的咖啡,彼此探討一下JAVA技術,交流工作經驗,分享JAVA帶來的快樂!本網站部分轉載文章,如果有版權問題請與我聯系。

          BlogJava 首頁 新隨筆 聯系 聚合 管理
            447 Posts :: 145 Stories :: 368 Comments :: 0 Trackbacks
          <script language=javascript>
          <!--

          String.prototype.len=function(){
          return this.replace(/[^\x00-\xff]/g,"**").length;
          }

          //Set maxlength for multiline TextBox
          function setMaxLength(object,length)
          {
          var result = true;
          var controlid = document.selection.createRange().parentElement().id;
          var controlValue = document.selection.createRange().text;
          if (controlid == object.id && controlValue != "")
          {
          result = true;
          }
          else if (object.value.len() >= length)
          {
          result = false;
          }
          if (window.event)
          {
          window.event.returnValue = result;
          return result;
          }
          }

          //Check maxlength for multiline TextBox when paste
          function limitPaste(object,length)
          {
          var tempLength = 0;
          if(document.selection)
          {
          if(document.selection.createRange().parentElement().id == object.id)
          {
          tempLength = document.selection.createRange().text.len();
          }
          }
          var tempValue = window.clipboardData.getData("Text");
          tempLength = object.value.len() + tempValue.len() - tempLength;
          if (tempLength > length)
          {
          tempLength -= length;
          //alert(tempLength);
          //alert(tempValue);
          var tt="";
          for(var i=0;i<tempValue.len()-tempLength;i++)
          {
          if(tt.len()<(tempValue.len()-tempLength))
          tt=tempValue.substr(0,i+1);
          else
          break;
          }
          tempValue=tt;
          window.clipboardData.setData("Text", tempValue);
          }

          window.event.returnValue = true;
          }

          //-->
          </script>

          然后設多行的textbox或textarea的2個屬性.
          onkeypress="javascript:setMaxLength(this,100);" onpaste="limitPaste(this, 100)"
          現在好了,可以自動區分中英文了,這個方案不錯,供大家分享。
          posted on 2007-12-20 10:34 rogerfan 閱讀(4244) 評論(1)  編輯  收藏 所屬分類: 【JS/HTML】

          Feedback

          # re: JS限制textbox或textarea輸入字符長度[未登錄] 2009-05-12 21:49 lyy
          謝謝了,好用!  回復  更多評論
            

          主站蜘蛛池模板: 郸城县| 普陀区| 绥阳县| 胶州市| 准格尔旗| 泰来县| 岫岩| 梅州市| 旌德县| 沙洋县| 镶黄旗| 三原县| 化德县| 厦门市| 连云港市| 宜良县| 嘉义市| 澜沧| 济南市| 广丰县| 诸城市| 吉林省| 湘潭县| 永安市| 青河县| 南江县| 南澳县| 浮梁县| 油尖旺区| 顺平县| 德格县| 无棣县| 牙克石市| 江口县| 通河县| 泰和县| 鲁甸县| 囊谦县| 来安县| 利津县| 长泰县|