1.限制只能输入正数和小数保留小数点后两位
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <input type="number" id="txtNum" /> 2 3 <script type="javascript"> 4 $(function(){ 5 $("#txtNum").keyup(function () { 6 $(this).val(ChangeNumValue($(this).val())); 7 }); 8 9 10 }); 11 12 function ChangeNumValue(tmpVal) 13 { 14 if (tmpVal) { 15 var tmpVal = tmpVal.replace(/[^d.]/g, ''); 16 var reg = /^(0|([1-9]d*))(.d{1,2})?$/; //正则验证保留 最多允许后输入两位小数 17 if (!reg.test(tmpVal)) { 18 tmpVal = tmpVal + ""; 19 tmpVal = tmpVal.substring(0, tmpVal.indexOf(".") + 3); 20 var n = (tmpVal.split('.')).length - 1; 21 if (n > 1) { 22 tmpVal = tmpVal.substring(0, tmpVal.indexOf(".")); 23 } 24 } 25 return tmpVal; 26 } else { 27 return ""; 28 } 29 } 30 </script>
2.只能输入正整数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <input type="text" onkeyup="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,'')}else{this.value=this.value.replace(/D/g,'')}" onafterpaste="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,'0')}else{this.value=this.value.replace(/D/g,'')}" />
3.输入正数和小数(保留两位)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <input type="number" step="0.01" min="0" onkeyup="this.value= this.value.match(/d+(.d{0,2})?/) ? this.value.match(/d+(.d{0,2})?/)[0] : ''">