<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <input type="text" name="je" onblur="clearNoNum(thi ...
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <input type="text" name="je" onblur="clearNoNum(this)"/>元 <script type="text/javascript"> function clearNoNum(obj) { obj.value = obj.value.replace(/[^\d.]/g, ""); //清除“數字”和“.”以外的字元 obj.value = obj.value.replace(/\.{2,}/g, "."); //只保留第一個. 清除多餘的 obj.value = obj.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", "."); obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3');//只能輸入兩個小數 if (obj.value.indexOf(".") < 0 && obj.value != "") {//以上已經過濾,此處控制的是如果沒有小數點,首位不能為類似於 01、02的金額 obj.value = parseFloat(obj.value); } if (!obj.value || obj.value == '0' || obj.value == '0.0' || obj.value == '0.00') { alert('退款金額不能為空'); return; } // 正常得話繼續調後端介面 } </script> </body> </html>