我想只得到正的值,有什么方法来防止它只使用html 请不要建议验证方法


当前回答

像这样使用min属性:

<input type="number" min="0">

其他回答

< input type = "数量" name = " credit_days "模式= ^ \[-]+” # credit_days = " ngModel " class = "表单控件” 占位符="{{'输入信用天数' |翻译}}" min="0" [(ngModel)] = " provider.credit_days " onkeypress = "返回(事件。charCode == 8 ||事件。charCode == 0 || 事件。charCode == 13) ?Null:事件。charCode >= 48 &&事件。charCode < = 57" onpaste="return false"> .

这里有一个解决方案,我工作最好的一个QTY字段,只允许数字。

// Only allow numbers, backspace and left/right direction on QTY input
    if(!((e.keyCode > 95 && e.keyCode < 106) // numpad numbers
        || (e.keyCode > 47 && e.keyCode < 58) // numbers
        || [8, 9, 35, 36, 37, 39].indexOf(e.keyCode) >= 0 // backspace, tab, home, end, left arrow, right arrow
        || (e.keyCode == 65 && (e.ctrlKey === true || e.metaKey === true)) // Ctrl/Cmd + A
        || (e.keyCode == 67 && (e.ctrlKey === true || e.metaKey === true)) // Ctrl/Cmd + C
        || (e.keyCode == 88 && (e.ctrlKey === true || e.metaKey === true)) // Ctrl/Cmd + X
        || (e.keyCode == 86 && (e.ctrlKey === true || e.metaKey === true)) // Ctrl/Cmd + V
    )) {
        return false;
    }

我想允许小数,如果输入负数,不清除整个输入。这在铬至少工作良好:

<input type="number" min="0" onkeypress="return event.charCode != 45">

像这样使用min属性:

<input type="number" min="0">

这个问题的答案是没有帮助的。因为它只在你使用上/下键时起作用,但如果你输入-11,它就不起作用。这是我用的一个小方法

这个是整数

  $(".integer").live("keypress keyup", function (event) {
    //    console.log('int = '+$(this).val());
    $(this).val($(this).val().replace(/[^\d].+/, ""));
    if (event.which != 8 && (event.which < 48 || event.which > 57))
    {
        event.preventDefault();
    }
   });

当你有价格的时候

        $(".numeric, .price").live("keypress keyup", function (event) {
     //    console.log('numeric = '+$(this).val());
    $(this).val($(this).val().replace(/[^0-9\,\.]/g, ''));

    if (event.which != 8 && (event.which != 44 || $(this).val().indexOf(',') != -1) && (event.which < 48 || event.which > 57)) {
        event.preventDefault();
    }
   });