zoukankan      html  css  js  c++  java
  • 前端判断输入框中是数字的正则表达式

    工作中有不同的判断要求,一般是判断输入框是正整数或者是正的小数,但是有的判断还要加能输入空也就是不输入,这就是现在要解决的问题。

    var tt=/^(?:(?!0)d*|0)(?:.d+)?$/;//能输入空格和正数包括小数
    

    而一般不能输入空的正数判断:

    var tt=/^[0-9]+.?[0-9]*$/;//能输入正数包括小数
    

    不妨可以试试

    <html>
    	<head>
    		<meta charset="UTF-8">
    		<title></title>
    	<script src="http://apps.bdimg.com/libs/jquery/2.0.1/jquery.min.js"></script>
    	</head>
    	<body>
    		<input type="text" id="input1" />
    		<input type="text" id="input2" />
    		<table>
    		<tr><td><div id="a1">1</div></td></tr>
    		<div id="a2">2</div>
    		<div id="a3">3</div>
    		<div id="a4">4</div>
    		</table>
    		<script type="text/javascript">
    			$('div[id^="a"]').each(function(){
    				 if($(this).val()!=""){
    				 	console.log($(this).val());
    				 }
    			});
    			//var tt=/^(?:(?!0)d*|0)(?:.d+)?$/;//能输入空格和正数包括小数
    			var tt=/^[0-9]+.?[0-9]*$/;//能输入正数包括小数
    			$("#input1").blur(function () {
    				var int1=$("#input1").val();
    				console.log(int1)
    				if(!tt.test(int1)){
    					alert("请输入数字");
    				}
    			});
    		</script>
    	</body>
    </html>
    

      

  • 相关阅读:
    DDD 领域驱动设计-谈谈 Repository、IUnitOfWork 和 IDbContext 的实践
    UVA10071 Back to High School Physics
    UVA10071 Back to High School Physics
    UVA10055 Hashmat the Brave Warrior
    UVA10055 Hashmat the Brave Warrior
    UVA458 The Decoder
    UVA458 The Decoder
    HDU2054 A == B ?
    HDU2054 A == B ?
    POJ3414 Pots
  • 原文地址:https://www.cnblogs.com/feipengting/p/8387627.html
Copyright © 2011-2022 走看看