1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html xmlns="http://www.w3.org/1999/xhtml"><head> 3 <meta http-equiv="Content-Type" content="text/html; charset=gbk"> 4 <title>判断数字是否为两位数</title> 5 <style type="text/css"> 6 body{font:12px/1.5 arial;text-align:center;} 7 .f-text{width:50px;border:1px solid #ccc;background:#f0f0f0;font-family:inherit;padding:3px;margin-right:10px;} 8 </style> 9 </head> 10 <body> 11 <input class="f-text" type="text" /> 12 <input value="是否为两位数" type="button" /> 13 <script type="text/javascript"> 14 window.onload = function(){ 15 var inputs = document.getElementsByTagName("input"); 16 var span = document.getElementsByTagName("span")[0]; 17 18 var size = inputs.length; 19 inputs[0].onkeyup = function(){ 20 this.value = this.value.replace(/[^d]/,""); 21 }; 22 inputs[1].onclick = function(){ 23 //(inputs[0].value.length == 2)?alert("是2位数"):alert("不是2位数"); 24 (inputs[0].value == "")?alert("不能为空"): 25 (/^[0-9]{2}$/.test(inputs[0].value))?alert("是2位数"):alert("是"+inputs[0].value.length+"位数"); 26 }; 27 28 29 } 30 </script> 31 </body> 32 </html>