1. 程式人生 > >jq 判斷值只能輸入數字和小數點

jq 判斷值只能輸入數字和小數點

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
  <input type="text" id="i">
  <button id="btn">button</button>
</body>
</html>
<script>


//$("#btn").click(function(){
//  var oI=$("#i").val();
//  if(parseFloat(oI)||parseFloat(oI)&&(oI!==oI)){
//    alert(oI)
//  }else{
//    alert('非數字')
//  }
//})

$("#btn").click(function(){
  var oI=$("#i").val();
  if(!parseFloat(oI)||!parseFloat(oI)&&(oI===oI)){
    alert('非數字')
  }
})



</script>

方法二:

 var reg = new RegExp("^[0-9]+([.]{1}[0-9]+){0,1}$");  
var price=$(this).parent().siblings("td").children("input[name='price[]']").val();
if (!reg.test(price)) {  
	alert("錯誤:斷金額只能是數字和小數點空!"); 
	$(this).parent().siblings("td").children("input[name='price[]']").focus();
	return false;	
}