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

Time:12-13

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>

CodePudding user response:

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>

  • Related