Home  >  Article  >  Web Front-end  >  How to restrict the HTML text box input to only numbers and decimal points

How to restrict the HTML text box input to only numbers and decimal points

高洛峰
高洛峰Original
2017-03-28 09:48:553051browse

This article mainly introduces the method of restricting the input of HTML text box to only numbers and decimal points. Has very good reference value. Let’s take a look with the editor below

Code:

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

PS: js realizes that only numbers can be entered and decimal point text box

<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>

The above is the detailed content of How to restrict the HTML text box input to only numbers and decimal points. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn