Reprint: html+js implements an input box that only allows two decimals to be entered

JS code:

<script language="JavaScript" type="text/javascript">

function clearNoNum(obj){

  obj.value = obj.value.replace(/[^\d.]/g,""); // Remove characters other than " number " and "."

  obj.value = obj.value.replace(/^\./g,""); // Verify that the first character is a number and not a .

  obj.value = obj.value.replace(/\.{2,}/g,"."); // Keep only the first one Clear the excess.

  obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");//Only allowed enter a decimal point

  obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3'); // Only two decimals can be entered

}

</script>

 

HTML code:

<input type="text" name="addoil" onkeyup="clearNoNum(this)" />

 

Original link: http://blog.sina.com.cn/s/blog_724008890101dgep.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325214627&siteId=291194637