<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title> 驗證數字最簡單正則表達式大全 </title> </head> <body> <h3>輸入完按回車後即可驗證!(自認為最簡單!)</h3> 正整數: <input type="text" size="20" onkeydown="if(event.keyCode == 13) alert(/^/d+$/.test(this.value));" /> <br /> 負整數: <input type="text" size="20" onkeydown="if(event.keyCode == 13) alert(/^-/d+$/.test(this.value));" /> <br /> 整 數: <input type="text" size="20" onkeydown="if(event.keyCode == 13) alert(/^-?/d+$/.test(this.value));" /> <br /> 正小數: <input type="text" size="20" onkeydown="if(event.keyCode == 13) alert(/^/d+/./d+$/.test(this.value));" /> <br /> 負小數: <input type="text" size="20" onkeydown="if(event.keyCode == 13) alert(/^-/d+/./d+$/.test(this.value));" /> <br /> 小 數: <input type="text" size="20" onkeydown="if(event.keyCode == 13) alert(/^-?/d+/./d+$/.test(this.value));" /> <br /> 實 數: <input type="text" size="20" onkeydown="if(event.keyCode == 13) alert(/^-?/d+(/./d+)?$/.test(this.value));" /> <br /> 保留1位小數:<input type="text" size="20" onkeydown="if(event.keyCode == 13) alert(/^-?/d+(/./d{1,1})?$/.test(this.value));" /> <br /> 保留2位小數:<input type="text" size="20" onkeydown="if(event.keyCode == 13) alert(/^-?/d+(/./d{1,2})?$/.test(this.value));" /> <br /> 保留3位小數:<input type="text" size="20" onkeydown="if(event.keyCode == 13) alert(/^-?/d+(/./d{1,3})?$/.test(this.value));" /> <br /> </body> </html>