Javascript 判斷整型、浮點型、去空格

來源:互聯網
上載者:User
 1 <script language="javascript">
 2     //判斷是否是整數
 3     function isNum(str) 
 4     {
 5         var i;
 6         for(i=0; i<str.length; i++) 
 7         {
 8             if (str.charAt(i) == ".") 
 9             {
10                 return false;
11             }
12         }
13         return !isNaN(str);
14     }
15                 
16     //整型判斷
17     function checkInt(e)
18     {
19         e.value = e.value.replace(/\s/g,""); 
20         
21         if(e.value.length > 0)
22         {
23             //有效數值判斷
24             if(isNaN(e.value))
25             {   
26                 alert('無效數值,請重新輸入!')
27                 e.value='';   
28                 setTimeout('document.all['+e.sourceIndex+'].focus()',10)   
29             }
30             
31             //正數判斷
32             if(e.value < 0)
33             {
34                 alert("不能小於0,請重新輸入!"); 
35                 e.value='';  
36                 setTimeout('document.all['+e.sourceIndex+'].focus()',10)
37             }
38             
39             //整數判斷
40             if(isNum(e.value) == false)
41             {
42                 alert("必須是整數,請重新輸入!"); 
43                 e.value='';  
44                 setTimeout('document.all['+e.sourceIndex+'].focus()',10)
45             }
46         }
47     }
48     
49     //浮點型判斷
50     function checkFloat(e)
51     {
52         e.value = e.value.replace(/\s/g,""); 
53         
54         if(e.value.length > 0)
55         {
56             //有效數值判斷
57             if(isNaN(e.value))
58             {   
59                 alert('無效數值,請重新輸入!')
60                 e.value='';   
61                 setTimeout('document.all['+e.sourceIndex+'].focus()',10)   
62             }
63             
64             //正數判斷
65             if(e.value < 0)
66             {
67                 alert("不能小於0,請重新輸入!"); 
68                 e.value='';  
69                 setTimeout('document.all['+e.sourceIndex+'].focus()',10)
70             }
71         }
72     }
73 </script>

 

 

 

1 <asp:textbox id="tb" runat="server" onblur="checkFloat(this)"></asp:textbox>

 

 

相關文章

聯繫我們

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