Multiplicity of values ​​in input

Good afternoon. Maybe someone knows how to make automatic rounding of the entered values ​​in the input with a message that the value has been changed. This function is needed to change the values ​​of the number of goods in the basket, so that it is not any number that the user enters, but a multiple of the number of goods in the package. Who knows, tell me, please!)


Answer 1, authority 100%

Solution with jQuery. I can write without it.

<input type="text" pack="4" />
<script type="text/javascript">
$('input[pack]').change(function(){ //        pack
    var val = ~~this.value || 0; //  
    var pack = ~~this.getAttribute('pack') || 0; //  -  
    if(val%pack != 0){
        this.value = val - val%pack; //  -
        //   - this.value = Math.ceil(val/pack)*pack;
        alert('   -  !');
    }
});
</script>

Updated code
Now the script works like this: set the pack attribute to the input – the number in the package and … that’s it. =) And it works.

Additive
If you need compatibility with another library, you can also add var j = jQuery.noConflict();and use j(…) instead of $(…) or just write jQuery(… ) instead of $(…) .


Answer 2, authority 33%

<html>
    <script>
        function pri() {
            var t=10;
            if (t%2 != 1){
                alert('');
            }
        }
    </script>
    <form action=# method=post>
        <input type="text" onchange="pri();">
    </form>
</html>

Answer 3, authority 33%

var quantity_per_pack = 5; //   
var amount = Math.ceil($('input#user_amount').val()/quantity_per_pack)*quantity_per_pack;
$('input#user_amount').val(amount);
alert(' ,     .');