常用的JAVAscript限制輸入字元數代碼

來源:互聯網
上載者:User
關鍵字 網頁製作 Ajax JavaScript

<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "HTTP://www.jzread.com/TR/xhtml1/DTD/ xhtml1-transitional.dtd">
<html xmlns="HTTP://www.jzread.com/1999/xhtml">
<head>
< meta HTTP-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css教程" media="screen">
body{ margin:0px; padding:0px;}
div{ width:400px; margin:0px auto;}
div h3{ background:#CCCCCC; font-size:14px; font-weight:bold; text-align:center; line-height:22px; margin:6px 0px; padding:0px;}
div textarea{ width:388px; height:120px; border:#999999 1px solid; margin:0px; padding:0px; font-size:12px; padding: 5px; line-height:16px;}
div p{ font-size:12px; margin:0px; line-height:22px;}
div p span{ color:#FF6600; font-weight:bold;}
</style>
<title>JAVAscript限制文字方塊輸入的最大字元數</title>
</head>

<body>
<div>
<h3>字元數測試</h3>
<textarea rows="20" cols="50" onkeypress="return checkMaxLength(this,20)"></textarea>
<p>最多<span>20</span>個字元。 </p>
</div>

<script type="text/javascript教程">
function checkMaxLength(oTextArea,maxLength){
return oTextArea.value.length!=maxLength;
}
</script>
</body>
</html>

方案二

<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "HTTP://www.jzread.com/TR/xhtml1/DTD/ xhtml1-transitional.dtd">
<html xmlns="HTTP://www.jzread.com/1999/xhtml">
<head>
< meta HTTP-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css" media="screen">
body{ margin:0px; padding:0px;}
div{ width:400px; margin:0px auto;}
div h3{ background:#CCCCCC; font-size:14px; font-weight:bold; text-align:center; line-height:22px; margin:6px 0px; padding:0px;}
div textarea{ width:388px; height:120px; border:#999999 1px solid; margin:0px; padding:0px; font-size:12px; padding: 5px; line-height:16px;}
div p{ font-size:12px; margin:0px; line-height:22px;}
div p span{ color:#FF6600; font-weight:bold;}
</style>
<title>JAVAscript限制文字方塊輸入的最大字元數</title>
</head>

<body>
<div>
<h3>字元數測試</h3>
<textarea rows="20" cols="50" onkeyup="checkMaxLength(this,20)" onpaste="checkMaxLength(this,20)"></ textarea>
<p>最多<span>20</span>個字元。 </p>
</div>

<script type="text/javascript">
function checkMaxLength(area, maxLength) {
if (area.value.length >maxLength) {
area.value = area.value.substr(0, maxLength);
}
}
</script>
</body>
</html>

方案三

<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "HTTP://www.jzread.com/TR/xhtml1/DTD/ xhtml1-transitional.dtd">
<html xmlns="HTTP://www.jzread.com/1999/xhtml">
<head>
< meta HTTP-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css" media="screen">
body{ margin:0px; padding:0px;}
div{ width:400px; margin:0px auto;}
div h3{ background:#CCCCCC; font-size:14px; font-weight:bold; text-align:center; line-height:22px; margin:6px 0px; padding:0px;}
div textarea{ width:388px; height:120px; border:#999999 1px solid; margin:0px; padding:0px; font-size:12px; padding: 5px; line-height:16px;}
div p{ font-size:12px; margin:0px; line-height:22px;}
div p span{ color:#FF6600; font-weight:bold;}
</style>
<title>JAVAscript限制文字方塊輸入的最大字元數</title>
</head>

<body>
<div>
<h3>字元數測試</h3>
<textarea rows="20" cols="50" onkeyup=" checkMaxLength(this,20)" onpaste="checkMaxLength(this,20)"></textarea>
<p>最多<span>20 </span>個字元,一個漢字2個字元。 </p>
</div>

<script type="text/javascript">
function checkMaxLength(area, maxLength) {
       var currentStr="";
      for(var i=0,len=area.value.length;i<len;i++) {
            currentStr+=area.value.charAt(i);
           if(getCharLength(currentStr)>maxLength){
                  area.value=area.value.substr(0,i);
                    return;
           }
     }
}

function getCharLength(str){
var charLen=0;
for(var i=0,len=str.length;i<len;i++){
if(str.charCodeAt(i)>255){
charLen+=2;
}else{
charLen+=1;
}
}
return charLen;
}
</script>
</body>
</html>

方案三

<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "HTTP://www.jzread.com/TR/xhtml1/DTD/ xhtml1-transitional.dtd">
<html xmlns="HTTP://www.jzread.com/1999/xhtml">
<head>
< meta HTTP-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css" media="screen">
body{ margin:0px; padding:0px;}
div{ width:400px; margin:0px auto;}
div h3{ background:#CCCCCC; font-size:14px; font-weight:bold; text-align:center; line-height:22px; margin:6px 0px; padding:0px;}
div textarea{ width:388px; height:120px; border:#999999 1px solid; margin:0px; padding:0px; font-size:12px; padding: 5px; line-height:16px;}
div p{ font-size:12px; margin:0px; line-height:22px;}
div p span{ color:#FF6600; font-weight:bold;}
</style>
<title>JAVAscript限制文字方塊輸入的最大字元數</title>
</head>

<body>
<div>
<h3>字元數測試</h3>
<textarea rows="20" cols="50" onkeyup=" checkMaxLength(this,20)" onpaste="return false;" ></textarea>
<p>最多<span>20</span>個字元,一個漢字2個字元,你還可以輸入<span>20</span> 字元</p>
</div>

<script type="text/javascript">
var s=document.getElementsByTagName("span")[1];
function checkMaxLength(area, maxLength) {
      var currentStr="";
   if(area.value.length==0){s.innerHTML=maxLength;}
      for(var i=0,len=area.value.length;i<len;i++) {
            currentStr+=area.value.charAt(i);
           if(getCharLength(currentStr)>maxLength){
                  area.value=area.value.substr(0,i);
                    return;
           }else{
              s.innerHTML=maxLength-getCharLength(currentStr);
     }
     }
}

function getCharLength(str){
     var charLen=0;
  for(var i=0,len=str.length;i <len;i++){
       if(str.charCodeAt(i)>255){
         charLen+=2;
    }else{
        charLen+=1;
    }  
  }
  return charLen;
}
</script>
</body>
</html>
 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.