I have a code where I need to reset the html form if the text entered into the textbox doesn't match the predefined ones. However if I use the reset() function, the form is reset even if the text matches the predefined text. How do I avoid this problem? here's my code.
function showData() {
var code= document.getElementById("bcno").value.trim();
switch(code)
{
case "WASTE1":
document.getElementById('display').innerHTML ="This Is Waste Type 1.";
break;
case "WASTE2":
document.getElementById('display').innerHTML ="This Is Waste Type 2.";
break;
case "WASTE3":
document.getElementById('display').innerHTML ="This Is Waste Type 3.";
break;
case "WASTE4":
document.getElementById('display').innerHTML ="This Is Waste Type 4.";
break;
default:
setTimeout(function(){document.getElementById("updateform").reset();}, 2000);
document.getElementById('display').innerHTML ="The text does not match. Form will be reset momentarily!!.";
break;
}
Heres's the html part.
<form action="dbupdate.php" method="post" id="updateform"
name="updateform">
<label for="bcno">Item ID: </label>
<input type="text" autofocus="autofocus" name="bcno" id="bcno"
oninput="showData()" autocomplete="off" /> <br /><br />
<div id="display"></div>
<label for="ino">Quantity: </label>
<input type="number" id="ino" name="ino" value="1" /><br /><br />
<input type="button" onclick="document.updateform.submit();"
value="Confirm"> </form>
bcno is the id of the textbox. What am i doing wrong? Also I am using Google Chrome (version 63.0.3239.132, the latest) to test my code
HTMLcode.<form action="dbupdate.php" method="post" id="updateform" name="updateform"> <label for="bcno">Item ID: </label> <input type="text" autofocus="autofocus" name="bcno" id="bcno" oninput="showData()" autocomplete="off" /> <br /><br /> <div id="display"></div> <label for="ino">Quantity: </label><input type="number" id="ino" name="ino" value="1" /><br /><br /> <input type="button" onclick="document.updateform.submit();" value="Confirm"> </form>