Miva Merchant Modules and Development
Want to start an online store? We work with you from start to finish, from commerce platform to design to SEO.
Experience counts, and we have a lot.

INPUT: Enforce Min/Max As You Type

Scot Ranney • September 23, 2024


Min/Max attributes don't work as you type, they only work for the arrows/wheel. Tiny bit of js fixes that.

<input type="number" min="0" max="23" value="14" onkeyup=enforceMinMax(this)>
<script>
function enforceMinMax(el) {
  if (el.value != "") {
	if (parseInt(el.value) < parseInt(el.min)) {
	  el.value = el.min;
	}
	if (parseInt(el.value) > parseInt(el.max)) {
	  el.value = el.max;
	}
  }
}
</script>

You could use jquery show to display a message if it goes out of range, maybe a reminder what the ranges should be.


https://www.scotsscripts.com/mvblog/input-enforce-minmax-as-you-type.html

mvkb_input