做项目时自己写一段js给大家。关于文本限制字数的问题,在实际开发中经常用到;主要问题出现在对中文的限制,下面代码就解决关于限制字节数的校验问题;只要将此下代码保存到一个js文件中并引入到校验的页面中,便可使用!同时希望大家给与大力支持和宝贵意见,本人会在今后闲余之际,发表更多的好文章,谢谢!!以下是引用片段:
/*
value: 值;
byteLength:数据库字节长度
title:字段中文名称
attribute:属性名称
使用方法说明: 添加 (1) onkeyup="limitLength(this.value,100,'名称','name')"
(2) id="name" 或【struts标签】styleId="name"
注意:id名称和 attribute属性名称要一样
例子:<textarea name="explain" id="explain" onkeyup="limitLength(value,5,'语义说明','explain')" > 或 <input type="text" name="explain" id="explain" onkeyup="limitLength(value,5,'语义说明','explain')" > */ function limitLength(value, byteLength, title, attribute) {
var newvalue = value.replace(/[^\x00-\xff]/g, "**");
var length = newvalue.length;
//当填写的字节数小于设置的字节数
if (length * 1 <=byteLength * 1){
return;
}
var limitDate = newvalue.substr(0, byteLength);
var count = 0;
var limitvalue = "";
for (var i = 0; i < limitDate.length; i++) {
var flat = limitDate.substr(i, 1);
if (flat == "*") {
count++;
}
}
var size = 0;
var istar = newvalue.substr(byteLength * 1 - 1, 1);//校验点是否为“×”
//if 基点是×; 判断在基点内有×为偶数还是奇数
if (count % 2 == 0) {
//当为偶数时
size = count / 2 + (byteLength * 1 - count);
limitvalue = value.substr(0, size);
} else {
//当为奇数时
size = (count - 1) / 2 + (byteLength * 1 - count);
limitvalue = value.substr(0, size);
}
alert(title + "最大输入" + byteLength + "个字节(相当于"+byteLength /2+"个汉字)!");
document.getElementById(attribute).value = limitvalue;
return;
}
转http://dev.yesky.com/
/*
value: 值;
byteLength:数据库字节长度
title:字段中文名称
attribute:属性名称
使用方法说明: 添加 (1) onkeyup="limitLength(this.value,100,'名称','name')"
(2) id="name" 或【struts标签】styleId="name"
注意:id名称和 attribute属性名称要一样
例子:<textarea name="explain" id="explain" onkeyup="limitLength(value,5,'语义说明','explain')" > 或 <input type="text" name="explain" id="explain" onkeyup="limitLength(value,5,'语义说明','explain')" > */ function limitLength(value, byteLength, title, attribute) {
var newvalue = value.replace(/[^\x00-\xff]/g, "**");
var length = newvalue.length;
//当填写的字节数小于设置的字节数
if (length * 1 <=byteLength * 1){
return;
}
var limitDate = newvalue.substr(0, byteLength);
var count = 0;
var limitvalue = "";
for (var i = 0; i < limitDate.length; i++) {
var flat = limitDate.substr(i, 1);
if (flat == "*") {
count++;
}
}
var size = 0;
var istar = newvalue.substr(byteLength * 1 - 1, 1);//校验点是否为“×”
//if 基点是×; 判断在基点内有×为偶数还是奇数
if (count % 2 == 0) {
//当为偶数时
size = count / 2 + (byteLength * 1 - count);
limitvalue = value.substr(0, size);
} else {
//当为奇数时
size = (count - 1) / 2 + (byteLength * 1 - count);
limitvalue = value.substr(0, size);
}
alert(title + "最大输入" + byteLength + "个字节(相当于"+byteLength /2+"个汉字)!");
document.getElementById(attribute).value = limitvalue;
return;
}
转http://dev.yesky.com/
解决方案 »
- 给我什么理由去用JQUERY?
- 一个限定条件的问题
- js能取得当前IE中的查看菜单中编码项中的值吗?
- 菜鸟发问,怎么样用JAVASCRIPT来获取一个图片或者其他文件的属性啊,比如文件的修改时间,大小等等属性值啊,高手帮忙
- 在jsp中或javascript中有没有一个方法把弹出的新窗口总显示在最上面?在线等!
- 求教HTMLElement中class属性获取方法
- 菜鸟的一个简单问题:两个.js文件之间的函数调用问题!
- 如何中断一个表单提交请求?
- 怎样隐藏地址栏合工具栏?
- 头痛的问题
- 打开一个主页,然后又打开了一些次页,当网页关闭的剩下一个网页时,关闭这个网页时弹出提示框!
- 当鼠标移动到下拉列表时显示列表内容
function limitLength(value, byteLength, title, attribute)
{
var newbytelength = byteLength;
var array = new Array();
var newvalue = value.replace(/[^\x00-\xff]/g, function(){array.push(arguments[0]);return "**";});
var length = newvalue.length;
//当填写的字节数小于设置的字节数
if (parseInt(length) <= parseInt(byteLength))
{
return true;
}
if (newvalue.charAt(newbytelength - 1) == "*")
{
newbytelength--;
}
newvalue = newvalue.substr(0, newbytelength);
var i = 0;
newvalue = newvalue.replace(/\*\*/g, function(){return array[i++];});
alert("[" + title + "]最大输入" + byteLength + "个字节(相当于" + byteLength /2 + "个汉字)!\n原值:" + value + "\n现值:" + newvalue);
//document.getElementById(attribute).value = newvalue;
}
limitLength("中国人中国人中国人中国人中国人中国人中国人中国人", 19, "xxx", "")
function limitLength(value, byteLength, title, attribute)
{
var newbytelength = 0, i, charcode;
for (i = 0; i < value.length && newbytelength < byteLength; i++)
{
charcode = value.charCodeAt(i);
newbytelength++;
if (charcode > 255 || charcode < 0)
{
if (newbytelength >= byteLength)
{
break;
}
else
{
newbytelength++;
}
}
}
var newvalue = value.substr(0, i);
alert("[" + title + "]最大输入" + byteLength + "个字节(相当于" + byteLength /2 + "个汉字)!\n原值:" + value + "\n现值:" + newvalue);
//document.getElementById(attribute).value = newvalue;
}
limitLength("中国人中国人中国人中国人中国人中国人中国人中国人", 19, "xxx", "")