首页 > 编程语言 > 详细

JavaScript示例四(输入框数字格式检测)

时间:2014-11-27 10:50:33      阅读:272      评论:0      收藏:0      [点我收藏+]
<!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.onfocus=function(){
			if(textbox.style.backgroundColor != "red"){
				textbox.style.backgroundColor="yellow";
			}
		}

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

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


本文出自 “空空如也” 博客,请务必保留此出处http://6738767.blog.51cto.com/6728767/1583073

JavaScript示例四(输入框数字格式检测)

原文:http://6738767.blog.51cto.com/6728767/1583073

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!