Javascript实现真实字符串剩余字数提示的实例代码
更新时间:2013年10月22日 14:49:04 作者:
这篇文章介绍了Javascript实现真实字符串剩余字数提示的实例代码,有需要的朋友可以参考一下
复制代码 代码如下:
//文本框剩余字数提示(字符大小)
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>
相关文章
Javascript遍历Html Table示例(包括内容和属性值)
这篇文章主要介绍了Javascript如何遍历Html Table(包括内容和属性值),需要的朋友可以参考下2014-07-07
浅析jsopn跨域请求原理及cors(跨域资源共享)的完美解决方法
由于同源策略的缘故,ajax不能向不同域的网站发出请求。接下来通过本文给大家介绍jsopn跨域请求原理及cors(跨域资源共享)的完美解决方法,需要的朋友可以参考下2017-02-02


最新评论