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