js form表單input框限制20個(gè)字符,10個(gè)漢字代碼實(shí)例
直接粘貼到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> // 這是第一種最尋常的方法 //定義一個(gè)函數(shù) function checkField(value, byteLength, attribute) { // value是input框的值,byteLength是需求限制的字符,attribute是input的id名 var newvalue = value.replace(/[^\x00-\xff]/g, "**"); var length = newvalue.length; //當(dāng)填寫的字節(jié)數(shù)小于設(shè)置的字節(jié)數(shù) 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); //校驗(yàn)點(diǎn)是否為“×” //if 基點(diǎn)是×; 判斷在基點(diǎn)內(nèi)有×為偶數(shù)還是奇數(shù) if (count % 2 == 0) { //當(dāng)為偶數(shù)時(shí) size = count / 2 + (byteLength * 1 - count); limitvalue = value.substr(0, size); } else { //當(dāng)為奇數(shù)時(shí) size = (count - 1) / 2 + (byteLength * 1 - count); limitvalue = value.substr(0, size); } alert( "最大輸入" + byteLength + "個(gè)字節(jié)(相當(dāng)于" + byteLength / 2 + "個(gè)漢字)!" ); 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;">職業(yè):</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個(gè)字符,10個(gè)漢字詳解整合,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- js中forEach,for in,for of循環(huán)的用法示例小結(jié)
- thinkphp5 + ajax 使用formdata提交數(shù)據(jù)(包括文件上傳) 后臺(tái)返回json完整實(shí)例
- js遍歷詳解(forEach, map, for, for...in, for...of)
- 詳解Js里的for…in和for…of的用法
- js利用for in循環(huán)獲取 一個(gè)對(duì)象的所有屬性以及值的實(shí)例
- node.js程序作為服務(wù)并在windows下開機(jī)自啟動(dòng)(用forever)
- JS使用for in有序獲取對(duì)象數(shù)據(jù)
相關(guān)文章
js使用文檔就緒函數(shù)動(dòng)態(tài)改變頁面內(nèi)容示例【innerHTML、innerText】
這篇文章主要介紹了js使用文檔就緒函數(shù)動(dòng)態(tài)改變頁面內(nèi)容,結(jié)合實(shí)例形式分析了JavaScript使用innerHTML、innerText函數(shù)動(dòng)態(tài)操作頁面元素相關(guān)使用技巧,需要的朋友可以參考下2019-11-11JavaScript限定范圍拖拽及自定義滾動(dòng)條應(yīng)用(3)
這篇文章主要介紹了JavaScript限定范圍拖拽及自定義滾動(dòng)條應(yīng)用的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05JS 頁面自動(dòng)加載函數(shù)(兼容多瀏覽器)
兼容多個(gè)瀏覽器的自動(dòng)加載函數(shù)。2009-05-05JSON.stringify轉(zhuǎn)換JSON時(shí)日期時(shí)間不準(zhǔn)確的解決方法
這篇文章主要介紹了JSON.stringify轉(zhuǎn)換JSON時(shí)日期時(shí)間不準(zhǔn)確的解決方法,即JSON數(shù)據(jù)中包含日期對(duì)象時(shí),在轉(zhuǎn)換時(shí)會(huì)轉(zhuǎn)換成國(guó)際時(shí)間,而不是中國(guó)的時(shí)區(qū),需要的朋友可以參考下2014-08-08js通過Date對(duì)象實(shí)現(xiàn)倒計(jì)時(shí)動(dòng)畫效果
這篇文章主要為大家詳細(xì)介紹了js通過Date對(duì)象實(shí)現(xiàn)倒計(jì)時(shí)動(dòng)畫效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10