<input type="text" value="@item.CostCash.Value.ToString("#0.00")" class="cost" onKeyUp="amount(this)" onBlur="overFormat(this)" /> /** * 實時動態強制更改用戶錄入 * arg1 inputObject **/ function amount(th) { var regStrs = [ ['^0(\\d+)$', '$1'], //禁止錄入整數部分兩位以上,但首位為0 ['[^\\d\\.]+$', ''], //禁止錄入任何非數字和點 ['\\.(\\d?)\\.+', '.$1'], //禁止錄入兩個以上的點 ['^(\\d+\\.\\d{2}).+', '$1'] //禁止錄入小數點后兩位以上 ]; for (var i = 0; i < regStrs.length; i++) { var reg = new RegExp(regStrs[i][0]); th.value = th.value.replace(reg, regStrs[i][1]); } } /** * 錄入完成后,輸入模式失去焦點后對錄入進行判斷並強制更改,並對小數點進行0補全 * arg1 inputObject **/ function overFormat(th) { var v = th.value; if (v === '') { v = '0.00'; } else if (v === '0') { v = '0.00'; } else if (v === '0.') { v = '0.00'; } else if (/^0+\d+\.?\d*.*$/.test(v)) { v = v.replace(/^0+(\d+\.?\d*).*$/, '$1'); v = inp.getRightPriceFormat(v).val; } else if (/^0\.\d$/.test(v)) { v = v + '0'; } else if (!/^\d+\.\d{2}$/.test(v)) { if (/^\d+\.\d{2}.+/.test(v)) { v = v.replace(/^(\d+\.\d{2}).*$/, '$1'); } else if (/^\d+$/.test(v)) { v = v + '.00'; } else if (/^\d+\.$/.test(v)) { v = v + '00'; } else if (/^\d+\.\d$/.test(v)) { v = v + '0'; } else if (/^[^\d]+\d+\.?\d*$/.test(v)) { v = v.replace(/^[^\d]+(\d+\.?\d*)$/, '$1'); } else if (/\d+/.test(v)) { v = v.replace(/^[^\d]*(\d+\.?\d*).*$/, '$1'); ty = false; } else if (/^0+\d+\.?\d*$/.test(v)) { v = v.replace(/^0+(\d+\.?\d*)$/, '$1'); ty = false; } else { v = '0.00'; } } th.value = v; }