這篇文章給大家分享的是有關(guān)JavaScript如何限制輸入框只允許整數(shù)和小數(shù)點(diǎn)的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。
創(chuàng)新互聯(lián)是專業(yè)的五河網(wǎng)站建設(shè)公司,五河接單;提供成都網(wǎng)站制作、成都網(wǎng)站設(shè)計(jì),網(wǎng)頁(yè)設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行五河網(wǎng)站開(kāi)發(fā)網(wǎng)頁(yè)制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛(ài)的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來(lái)合作!
第一種方法:
<!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body style="text-align:center;" id="body"> <h2 id="h2" style="color:#ff311f;"> 創(chuàng)新互聯(lián) </h2> <p id="GFG_UP" style="font-size: 15px; font-weight: bold;"> </p> <form> 請(qǐng)輸入內(nèi)容: <input id="input" oninput="valid(this)" type="text"> </form> <br> <p id="GFG_DOWN" style="font-size: 23px; font-weight: bold; color: #ff311f; "> </p> <script> var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); el_up.innerHTML = "查看輸入是否有效"; var RegExp = new RegExp(/^\d*\.?\d*$/); var val = document.getElementById("input").value; function valid(elem) { if (RegExp.test(elem.value)) { val = elem.value; el_down.innerHTML = "輸入的是有效字符"; } else { elem.value = val; el_down.innerHTML = "輸入的是無(wú)效字符"; } } </script> </body> </html>
在上述代碼示例中:
RegExp用于驗(yàn)證輸入。
每次輸入一個(gè)字符時(shí),整個(gè)輸入都會(huì)與 RegExp 匹配以檢查有效性。
如果有效,則使字符有效并添加到輸入中,否則無(wú)效。
第二種方法:
<!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body style="text-align:center;" id="body"> <h2 id="h2" style="color:orange;"> 創(chuàng)新互聯(lián) </h2> <p id="GFG_UP" style="font-size: 15px; font-weight: bold;"> </p> <form> 請(qǐng)輸入內(nèi)容: <input id="input" onkeypress="return GFG_Fun(this, event)" type="text"> </form> <br> <p id="GFG_DOWN" style="font-size: 23px; font-weight: bold; color: orange; "> </p> <script> var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); el_up.innerHTML = "查看輸入是否有效"; function isValid(el, evnt) { var charC = (evnt.which) ? evnt.which : evnt.keyCode; if (charC == 46) { if (el.value.indexOf('.') === -1) { return true; } else { return false; } } else { if (charC > 31 && (charC < 48 || charC > 57)) return false; } return true; } function GFG_Fun(t, evnt) { var a = isValid(t, evnt); if (a) { el_down.innerHTML = "輸入的是有效字符"; } else { el_down.innerHTML = "輸入的是無(wú)效字符"; } return a; } </script> </body> </html>
在上述代碼示例中:
每次輸入一個(gè)字符時(shí),都會(huì)檢查這個(gè)字符是否有效。
這個(gè)例子也是檢查輸入的小數(shù)位數(shù),一個(gè)不能輸入2個(gè)小數(shù)。
如果字符有效,則添加到輸入中,否則無(wú)效。
感謝各位的閱讀!關(guān)于“JavaScript如何限制輸入框只允許整數(shù)和小數(shù)點(diǎn)”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
網(wǎng)頁(yè)標(biāo)題:JavaScript如何限制輸入框只允許整數(shù)和小數(shù)點(diǎn)
標(biāo)題鏈接:http://chinadenli.net/article24/gpjpce.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、移動(dòng)網(wǎng)站建設(shè)、品牌網(wǎng)站建設(shè)、品牌網(wǎng)站制作、外貿(mào)建站、關(guān)鍵詞優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)