dimanche 9 juin 2019

alert happening within if statement even though conditions are not met

I'm making a piece of code in which you need to enter your age in order to get the price of a ticket to an event. If an invalid age or a word is put in I need to alert them. My alert is showing up even if you put in a valid age.

<center><br>To figure out ticket cost please enter your age.<br><br>

    Age: <input type="text" id="age"><br><br>

<input type="button" value="Submit" onclick= price(this.form)>
</center>

</body>

<script>
   function price(){

    if(document.getElementById("age").value<8 || document.getElementById("age").value>25||isNaN(age)){
         alert("That is not a valid age.")
       }

    if(document.getElementById("age").value>=8 && document.getElementById("age").value<13){
        alert("Your ticket price would be $900. Please contact us if you want to purchase a ticket."); 
    }
    if(document.getElementById("age").value>=13 && document.getElementById("age").value<16){
        alert("Your ticket price would be $950. Please contact us if you want to purchase a ticket."); 
    }

    if(document.getElementById("age").value>=16 && document.getElementById("age").value<19){
       alert("Your ticket price would be $1000. Please contact us if you want to purchase a ticket."); 
    }

    if(document.getElementById("age").value>=19 && document.getElementById("age").value<26){
        alert("Your ticket price would be $1050. Please contact us if you want to purchase a ticket."); 
    }

   } 

</script>

Aucun commentaire:

Enregistrer un commentaire