僅限正整數輸入

來源:互聯網
上載者:User

在某些瀏覽器不支援type number時這個是一個很好的替代方案

 

 

 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  
2 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml">
4 <head>
5 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
6 <title>僅限正整數輸入</title>
7
8 </head>
9
10 <body>
11 <script>
12 //void function (window,undefined){
13 window.onload = function() {
14 var doc = document,list = doc.getElementsByTagName('input');
15
16
17 availableNumberInput(list);
18 }
19 function availableNumberInput(inputs) {
20 var len = inputs.length,i,reg = /^(8|9|4[8-9]|5[0-7]|9[6-9]|10[0-5])$/ig;
21
22 document.onmousedown=function(){
23 try{
24 document.selection.empty()
25 }catch(e){
26 getSelection().removeAllRanges()
27 }
28 }
29 for (i = len; i > 0; (inputs[--i].onkeydown = function(e) {
30 e = window.event || e;
31 var target = e.srcElement || e.target,code = e.charCode || e.keyCode;
32 reg.lastIndex = 0;
33 if (!reg.test(code) || e.shiftKey) {
34 return false;
35 }
36 target.value === '0' && (target.value = '');
37 },inputs[i].oncontextmenu = function() {
38 return false;
39 }),inputs[i].style.imeMode = 'disabled') {
40 }
41 }
42 //}(window);
43 </script>
44
45 拖個文字進輸入框看看<br/>
46 <input type="text"/><br/>
47 <input type="text"/><br/>
48 <input type="text"/>
49 </body>
50 </html>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.