[Demo] Restrict negative values into a TextBox

[Demo] Restrict negative values into a text box

by Joe

HTML

<input type="number" id="txtamountorhours" min="0" style="width:80px;" onkeypress="restrictMinus(event);" />

JavaScript

function restrictMinus(e) {
    var txtamountorhours = document.getElementById('txtamountorhours');

    if (txtamountorhours) {
        var inputKeyCode = e.keyCode ? e.keyCode : e.which;

        if (inputKeyCode != null) {
            if ( inputKeyCode >= 43 && inputKeyCode <= 47 ) e.preventDefault();
        }
    }
}

function blankOutTextBox() {
    document.getElementById('txtamountorhours').value = "";
}