使用javascript onblur事件默认输入框为0.00,如果没有输入值或如果一个值小于0

Use javascript onblur event to default input box to 0.00 if no value is entered or if a value is less than 0

本文关键字:输入 小于 如果 一个 事件 onblur javascript 默认 使用 如果没有      更新时间:2023-09-26

这是我的onblur事件与javascript到目前为止。在页面加载框中有自动加载的值,但如果用户删除该值或输入小于零的值,则默认为0.00。现在默认为NaN。

function checkformat(entry) {    
    test = entry.value;
    if (!isNaN(test)) {
        entry.value=parseFloat(entry.value).toFixed(2);
    }
    else if (isNaN(test) == true) {      
        test.value='0.00';        
    }
    else if (test < 0.00) {
        test.value = '0.00';
    }
    else {
      test.value = '0.00';
    }
    
  }
<input id='Line Item <% line %>' type="text" onkeyup="updateTotal()" placeholder="Amount" name="AmountPaying" class="field m w-input" value="<% r.AmountOwed %>" onblur="checkformat(this)">

使用以下函数来控制IsNaN

function isNumeric(n) {
  return !isNaN(parseFloat(n)) && isFinite(n);
}

新代码
function checkformat(entry) {    
    test = entry.value;
    if (isNumeric(test)) {
        entry.value=parseFloat(test).toFixed(2);
    }
...