Javascript实现真实字符串剩余字数提示_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > Javascript实现真实字符串剩余字数提示

Javascript实现真实字符串剩余字数提示

 2013/10/21 15:11:37  爱智旮旯  博客园  我要评论(0)
  • 摘要://文本框剩余字数提示(字符大小)functiontextLimitCheckSj(thisArea,maxLength,SpanId){varstr=thisArea.value;if(getChrLen(str,maxLength)>maxLength*2){thisArea.value=str.substring(0,x-1);}else{varvarss='(剩余字数:'+Math.floor((maxLength*2-getChrLen(str,maxLength))/2)+'
  • 标签:实现 Java 字符串 javascript
class="brush:javascript;gutter:false;">//文本框剩余字数提示(字符大小)
function textLimitCheckSj(thisArea, maxLength, SpanId) {
    var str = thisArea.value;
    if (getChrLen(str, maxLength) > maxLength * 2) {
        thisArea.value = str.substring(0, x-1);
    }
    else {
        var varss = '(剩余字数:' + Math.floor((maxLength * 2 - getChrLen(str, maxLength)) / 2) + ')';
        document.getElementById(SpanId).innerHTML = varss;
    }
}
function getChrLen(str, maxLength) {
    var realLength = 0, len = str.length, charCode = -1;
    x = 0;
    for (; (x < len) && (realLength <=maxLength * 2); x++) {
        charCode = str.charCodeAt(x);
        if (charCode >= 0 && charCode <= 128)
            realLength += 1;
        else
            realLength += 2;
    }
    return realLength;
}
<asp:TextBox ID="txtOwner_Name" runat="server" Width="200px" BackColor="LightYellow"
                        onkeyup="textLimitCheckSj(this,50,'spOwner_Name')"></asp:TextBox><span id="spOwner_Name"
                            style="color: #808080"><em>(50字以内)</em></span>

 

发表评论
用户名: 匿名