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,588
  • 10
  • 46
  • 79
user2650277
  • 6,289
  • 17
  • 63
  • 132

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
  • 85,780
  • 21
  • 159
  • 179
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,724
  • 5
  • 27
  • 39