1

I want to make the value in quantity show as 1 if its not valid.

<input type = "number" id = "quantity" value="1" onchange="Validatequantity()" />

function Validatequantity() {   
    var num = document.getElementById('quantity').value;
    if  (num < 1 || num > 10 ) {
        alert('Invalid ticket number ...Should be between 1-10');
        num = 1; // reset part is here(don't work)
    } 
}
Shaunak D
  • 20,236
  • 10
  • 45
  • 76
user2650277
  • 5,729
  • 16
  • 57
  • 120

2 Answers2

2
function Validatequantity() {
    var num = document.getElementById('quantity').value;
    if  (num < 1 || num > 10 ) {
        alert('Invalid ticket number ...Should be between 1-10');
        num = 1; // reset part is here(don't work)
        document.getElementById('quantity').value = 1; // Set input value to 1
        // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    } 
}
Tushar
  • 82,599
  • 19
  • 151
  • 169
1

If you want to work with the variable make it a reference to the element than its value:

function Validatequantity() {   
    var num = document.getElementById('quantity');
    if  (num.value < 1 || num.value > 10 ) {
        num.value = 1;
    } 
}
Samurai
  • 3,654
  • 5
  • 25
  • 38