<! 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>