| <!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> | |
| <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | |
| <title>js数字验证-细数逝去的过往</title> | |
| </head> | |
| <body> | |
| <script type="text/javascript"> | |
| function validate(){ | |
| var reg = new RegExp("^[0-9]*$"); | |
| var obj = document.getElementById("name"); | |
| if(!reg.test(obj.value)){ | |
| alert("请输入数字!"); | |
| } | |
| } | |
| </script> | |
| 请输入数字:<input type="text" id="name" /> | |
| <button id="button" onclick="validate();">提交</button> | |
| </body> | |
| </html> |