JSFiddle - React, Tailwind, and code Playground

HTML

<input id='quantity' name='quantity' type='number' min='0' max='5' />
<h2 id="res">

</h2>

JavaScript

function maxLength() {
   var obj = document.getElementById('quantity');
    var max = obj.getAttribute('max');
    var min = obj.getAttribute('min');
    if (obj.value > max || obj.value < min) {
        obj.setCustomValidity('Alert ');
    } else {
        obj.setCustomValidity('');
    }
}

document.getElementById('res').innerHTML = maxLength