js form表单input框限制20个字符,10个汉字代码实例
网络编程 2021-07-04 15:50www.168986.cn编程入门
这篇文章主要介绍了js form表单input框限制20个字符,10个汉字,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们狼蚁网站SEO优化随着长沙网络推广来一起学习学习吧
直接粘贴到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./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>
以上所述是长沙网络推广给大家介绍的js form表单input框限制20个字符,10个汉字详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,长沙网络推广会及时回复大家的。在此也非常感谢大家对狼蚁SEO网站的支持!
编程语言
- 机械手焊接机器人编程 机械手焊接设备编程
- vb数据库编程实例,vb做数据库
- tiobe世界编程语言排行榜,全球编程语言排行
- 成都java编程培训 成都it编程培训中心
- 编程基础培训,编程基础知识培训
- flash游戏编程基础教程,flash游戏开发教程
- 不会英语能学编程吗 不会英语可以学程序员吗
- 爱心代码编程C语言 爱心代码编程c语言公式
- c语言编程学习入门 c语言编程快速入门
- 西门子plc编程培训 西门子plc编程培训班
- 哪里可以学编程 台州哪里可以学编程
- 少儿编程哪个好 少儿编程哪个品牌好
- 编程是什么意思,图形编程是什么意思
- c语言编程软件下载,c语言编程软件在哪下载
- java编程软件下载,java编程下载手机版
- ug编程培训学校,ug编程培训教程