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>