Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Set min/max value for input field with CSS

Is there a way to set the min/max values of a HTML input form based on the ID with CSS?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can do this by getting the input’s DOM node:
Every time the input loses focus, determine whether the value is within the range you specified, if not, correct it and prompt

function change(){
        const searchBox = document.getElementById("myInput");
       if(searchBox.value>100){
      searchBox.value=100
      alert("the max value is 100")
      }
      if(searchBox.value<0){
        searchBox.value=0
        alert("the min value is 0")
      }
    }
<input type="text" value="" id="myInput" onBlur="change">

If you want to change the style by changing the ID:
Then you can first get his DOM through class, and then you can forcefully modify his id according to DOM.id

But it is not recommended to do so

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading