MIN-MAX: JS Auto Change min/max to Input Attribute

Miva Knowledge Base
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.
Important Notice: This information is for internal reference only. Use at your own risk.

MIN-MAX: JS Auto Change min/max to Input Attribute

Scot Ranney • August 22, 2024


<input type="number" min="0" max="23" value="14" onkeyup=enforceMinMax(this)> Run code snippet
<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>

https://www.scotsscripts.com/mvblog/min-max-js-auto-change-minmax-to-input-attribute.html

mvkb_input