限制输入字符数通用方法(汉字占2位,英符占1位)
程序员文章站
2022-07-05 17:27:09
(待完善补充) checkLimitCharacterLength(itemName, length, parentObj) { //校验字符长度(变量名,限制字符数,变量所属的对象) if (!parentObj) { parentObj = this; } let value = parentO ......
(待完善补充)
checklimitcharacterlength(itemname, length, parentobj) { //校验字符长度(变量名,限制字符数,变量所属的对象)
if (!parentobj) {
parentobj = this;
}
let value = parentobj[itemname];
let valuearray = value.split('');
let _len = 0
for (let i = 0, len = value.length; i < len; i++) {
if(_len < length) {
if(/[0-9a-za-z,\.\!@#$%^&\*\(\):";'\/`~\-=_\+]/.test(valuearray[i])) {
_len += 1
}
else {
_len += 2
}
}
else {
settimeout(() => {
parentobj[itemname] = value.substring(0, i);
}, 0)
break;
}
}
}