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

How can i check if the user's input value has a specific number?

as i am a real newbie, i want to check if the value that entered by the user is equal to a specific number.

I need to check if the value entered by the user is below 10 or not. I did the code below:

<form>
  <label for="value">Value</label>
  <input type="number" id="quantity" name="number"><br><br>
  <input type="button" value="Click me" onclick="myFunction()">
</form>
<p id="answer">answer here</p>
<script>

function myFunction() {
   var x= document.getElementbyId("quantity").value;
    if (x<10){
     document.getElementbyId("answer").innerHTML="The value is below 10"
}   else {
    document.getElementbyId("answer").innerHTML="The value is equal to or over 10";
  }
}
</script>

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

>Solution :

Your code is working correctly, just replace getElementbyId with getElementById. Below snippet is exactly your code with the replacement of getElementbyId.

<form>
  <label for="value">Value</label>
  <input type="number" id="quantity" name="number"><br><br>
  <input type="button" value="Click me" onclick="myFunction()">
</form>
<p id="answer">answer here</p>
<script>
  function myFunction() {
    var x = document.getElementById("quantity").value;
    if (x < 10) {
      document.getElementById("answer").innerHTML = "The value is below 10"
    } else {
      document.getElementById("answer").innerHTML = "The value is equal to or over 10";
    }
  }
</script>
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