前端之家收集整理的这篇文章主要介绍了
js form表单input框限制20个字符,10个汉字代码实例,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_
502_0@主要介绍了js form表单input框限制20个字符,10个汉字,文中通过示例
代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧@H_
502_0@直接粘贴到html
文件便可看到
效果<!DOCTYPE html>
<html>
<head>
<Meta charset="utf-8" />
<Meta http-equiv="X-UA-Compatible" content="IE=edge" />
<Meta name="viewport" content="width=device-width, initial-scale=1" />
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script>
// 这是第一种最寻常的方法
//定义一个函数
function checkField(value, byteLength, attribute) {
// value是input框的值,byteLength是需求限制的字符,attribute是input的id名
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(
"最大输入" +
byteLength +
"个字节(相当于" +
byteLength / 2 +
"个汉字)!"
);
document.getElementById(attribute).value = limitvalue;
return;
}
</script>
</head>
<body>
<div class="wrap">
<form>
<table class="list-style">
<tr>
<td style="width:15%;text-align:right;">职业:</td>
<td>
<input id="profession" type="text" "checkField(this.value,20,'profession')" class="textBox length-middle" name="profession"/>
<input id="interest" type="text" "checkField(this.value,100,'interest')" class="textBox length-middle" name="interest"/>
<input id="address" "checkField(this.value,40,'address')" type="text" class="textBox length-middle" name="address" />
</td>
</tr>
</table>
<div style="margin:20px 0 0 300px">
<button type="reset" style="width:60px;height:30px;margin-right:40px" "refresh()">重置</button>
<button type="submit" style="width:60px;height:30px" "return checkNull()">提交</button>
</div>
</form>
</div>
</body>
</html>
@H_
502_0@