☆蓝色梦想☆

世界总是反反覆覆错错落落地飘去 来不及叹息 生活不是平平淡淡从从容容的东西 不能放弃
posts - 57, comments - 5, trackbacks - 0, articles - 0

限制输入框的长度(汉字解决方案)

Posted on 2005-12-06 11:46 ☆蓝色梦想☆ 阅读(1039) 评论(1)  编辑  收藏 所属分类: WEB
<script>
function getStringUTFLength(str) {
    var value = str.replace(/[^\x00-\xff]/g,"  ");
    return value.length;
}

function leftUTFString(str,len) {
    if(getStringUTFLength(str)<=len)
        return str;
    var value = str.substring(0,len);

    while(getStringUTFLength(value)>len) {
        value = value.substring(0,value.length-1);
    }
    return value;
}

function count() {
    var value = document.getElementById("licenseother").value;
    value = value.replace(/[\u4e00-\u9fa5]/g,"  ");
    //alert(value.length);
    if(value.length>=255) {
        with(window.event) {
            cancelBubble = true;
            keyCode=0;
            returnValue = false;
        }
        document.getElementById("licenseother").value = leftUTFString(document.getElementById("licenseother").value,8);
    }
    document.getElementById("result").value = 255-getStringUTFLength(document.getElementById("licenseother").value);
}
</script>
<table width="100%">
<tr><td>
本输入框限制输入255个字符(汉字计算为2个字符:)
</td></tr>
<tr><td>
<textarea cols=80 rows=3 wrap="virtual" id="licenseother" onkeypress="count()" onkeyup="count()" onblur="count();" onChange="count();"></textarea>

</td></tr>
<tr><td>
剩余字符数:<input type="text" size="3" id="result" value="255">
</td></tr>
</table>
}

Feedback

# re: 限制输入框的长度(汉字解决方案)  回复  更多评论   

2013-08-15 15:17 by asdf
adggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg

只有注册用户登录后才能发表评论。


网站导航: