Validation of max number of items input box

Validate the maximum number of elements that can be added to an input box of numeric type either via keyboard, spinner, or arrow keys.

by Helmut Granda

HTML

<input class="maxNumberValidation" type="number" name="quantity" min="0" max="999" placeholder="0"/>

JavaScript

$('.maxNumberValidation').on('keyup keydown', function(e){
    console.log($(this).val() > 999)
        if ($(this).val() > 999 
            && e.keyCode != 46
            && e.keyCode != 8
           ) {
           e.preventDefault();     
           $(this).val(999);
        }
    });