<!doctype html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <meta name="Generator" content="EditPlus">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <title>输入框检测</title>
 </head>
 <body>
 <p>只允许输入数字,否则红色警示</p>
  <form action="http://localhost/index.jsp">
	<input type="textarea" id="textbox">
	<button type="submit" id="mybtn">提交</button>
  </form>
	<script>
		var textbox=document.forms[0].elements[0];
		
		//获取焦点事件
		textbox.οnfοcus=function(){
			if(textbox.style.backgroundColor != "red"){
				textbox.style.backgroundColor="yellow";
			}
		}

		//失去焦点事件
		textbox.οnblur=function(event){
			if(/[^\d]/.test(textbox.value)){
				textbox.style.backgroundColor="red";
			}else{
				textbox.style.backgroundColor="";
			}
		}
		
		//change事件
		textbox.οnchange=function(event){
			if(/[^\d]/.test(textbox.value)){
				textbox.style.backgroundColor="red";
			}else{
				textbox.style.backgroundColor="";
			}
		}

	</script>
 </body>
</html>