經典上的一位朋友提出的問題,偶以前正好寫過一個股票系統處理過一些貨幣資料,現在記憶猶在,所以就幫忙解決了。自己也儲存再次,以便以後使用。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title> new document </title> <meta name="generator" content="editplus" /> <meta name="author" content="" /> <meta name="keywords" content="" /> <meta name="description" content="" /> </head> <body> <script language="JavaScript" type="text/javascript"> <!-- function checkMoney(obj){ var tempValue=obj.value.replace(/(^\s+)|(\s+$)/g,'').replace('¥',''); if(!tempValue){return} if(/^-?\d+(\.\d+)?$/.test(tempValue)){ obj.value="¥"+parseFloat(tempValue).toFixed(2); }else{ alert('請輸入合法的貨幣值!'); return } } //--> </script> <input type="text" onchange="checkMoney(this)" size="20"> </body> </html>
[Ctrl+A 全選 注:如需引入外部Js需重新整理才能執行]