限制html文本框input只能输入数字和小数点

限制html文本框input只能输入数字和小数点,第1张

限制html文本框input只能输入数字和小数点

代码:

<input type="text" class="txt" name="qty" value="" onkeyup="this.value=this.value.replace(/[^0-9\.]/g,'')" />

PS:js实现只能输入数字和小数点的文本框

<html><head><meta http-equiv="content-Type" content="text/html;charset=gb2312"><title>js 只能输入数字和小数点</title>
<script language="JavaScript" type="text/javascript">
function clearNoNum(obj){   obj.value = obj.value.replace(/[^\d.]/g,"");  //清除“数字”和“.”以外的字符  
 obj.value = obj.value.replace(/^\./g,"");  //验证第一个字符是数字而不是. 
  obj.value = obj.value.replace(/\.{2,}/g,"."); //只保留第一个. 清除多余的.   
obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");

}
</script>
</head><body>只能输入数字和小数点的文本框:<input name="input1" onkeyup="clearNoNum(this)"></body></html>

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持脚本之家!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/web/611123.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-14
下一篇 2022-04-14

发表评论

登录后才能评论

评论列表(0条)

保存