在JavaScript中使用正則表達(dá)式來限制輸入框——輸入框中只能輸入數(shù)字

1
<script language="JavaScript">
2
function nst_convert_all(tinput)
3
{
4
if(tinput.value==""){
5
return;
6
}
7
var ms = tinput.value.replace(/[^\d\.]/g,"").replace(/(\.\d{2}).+$/,"$1").replace(/^0+([1-9])/,"$1").replace(/^0+$/,"0");
8
tinput.value = ms;
9
}
10
</script>
這樣使用:

1 <input type=“text” style="width:120px" onkeyup="nst_convert_all(this)"/>
請在這里測試

1

2

3

4

5

6

7

8

9

10


1 <input type=“text” style="width:120px" onkeyup="nst_convert_all(this)"/>