JS限制文字框只能輸入數字

來源:互聯網
上載者:User

標籤:style   blog   io   ar   color   使用   sp   on   div   

通過鍵盤每個鍵的key值進行限制,在html代碼使用事件onkeydown="return validateDigitsOnly(event)"即可實現

 1 /** 2  * 限制文字框只能輸入數字 3  * @constructor 4  */ 5 function validateDigitsOnly(evt) { 6     var e = evt || window.event, 7         key = e.keyCode || e.which; 8  9     if (10 // Backspace, Tab, Enter, Esc, Delete11         key == 8 || key == 9 || key == 13 || key == 27 || key == 46 ||12 // Ctrl + A13         (key == 65 && event.ctrlKey === true) ||14 // Home, End, 四個方向鍵15         key >= 35 && key <= 40) {16         return;17     }18 19     if (e.shiftKey || e.altKey || e.ctrlKey) {20         return false;21     }22 23     var el = e.target || e.srcElement,24 // 最大數字長度25         nl = el.getAttribute("data-numbers") || 15,26 // 最大小數長度27         dl = el.getAttribute("data-decimals") || 2,28         val = el.value,29 // "." 位置30         dotIndex = val.indexOf("."),31         rng = caret.call(el),32 // 游標在"."左邊33         rLeft = rng.end <= dotIndex,34 // 游標在"."右邊35         rRight = rng.begin > dotIndex;36 37     if (38 // 數字39         key >= 48 && key <= 57 ||40 // 數字鍵台輸入的數字41         key >= 96 && key <= 105) {42         if (validateValue(dotIndex, val, rLeft, rRight, nl, dl))43             return;44 45 // 選中部分文本再做一次處理46         val = val.substring(0, rng.begin) + val.substring(rng.end);47         dotIndex = val.indexOf(".");48         if (validateValue(dotIndex, val, rLeft, rRight, nl, dl))49             return;50     }51     else if (52 // ".", ","53         (key == 190 /*|| key == 188*/ ||54 // 數字鍵台上的 ".", ","55             key == 110/*|| key == 109*/) &&56 // 允許輸入小數57         dl > 0) {58         if (59 // 未輸入".", 且輸入的位置後面的小數位元未達到上限60             dotIndex == -1 && (rng.end == val.length || val.substring(rng.end).length <= dl) ||61 // 輸過".", 且選中部分文本包含"."62             dotIndex > -1 && rng.begin <= dotIndex && dotIndex < rng.end)63             return;64     }65 66     return false;67 }68 69 // 驗證輸入的值70 function validateValue(dotIndex, val, rLeft, rRight, nl, dl) {71     if (72 // 未輸入過"."73         dotIndex == -1 && val.length < nl ||74 // 游標位置在"."之前75         rLeft && val.substring(0, dotIndex).length < nl ||76 // 游標在"."之後且未達到小數上限77         rRight && val.substring(dotIndex + 1).length < dl)78         return true;79     return false;80 }81 82 // 擷取游標位置83 function caret() {84     var begin, end;85     if (this.setSelectionRange) {86         begin = this.selectionStart;87         end = this.selectionEnd;88     } else if (document.selection && document.selection.createRange) {89         var range = document.selection.createRange();90         begin = 0 - range.duplicate().moveStart(‘character‘, -100000);91         end = begin + range.text.length;92     }93     return { begin: begin, end: end };94 }

 

JS限制文字框只能輸入數字

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.