how can I alter this code to remove javascript error message when correct value is entered in text field [duplicate]
This script enables a submit button only if numbers are entered into the text field. onkeyup if the field is empty or non-numeric values are used the button remains disabled and an error message is displayed. All works ok, but when numeric values are entered and the button is enabled, the error message is still visible. I am searching for a method to clear the error message as the numeric values are entered in the text box. I would be very grateful for any pointers. html
<input type="text" id="numberTest" />
<button id="button" class="formButton" type="submit">Click Me</button>
<div id="error"></div>
javascript
var formButton = document.getElementById("button");
formButton.disabled = true;
var myInput = document.getElementById("numberTest");
const errorElement =document.getElementById('error');
myInput.onkeyup = function() {
let message = [];
var numberTest = parseInt(document.getElementById("numberTest").value);
if (Number.isNaN(numberTest) || numberTest == "" || numberTest === null) {
formButton.disabled = true; // return disabled as true whenever the input field is empty
message.push('Numeric values only cannot be empty');
} else {
formButton.disabled = false; // enable the button once the input field has content
}
if (message.length > 0) {
errorElement.innerText=message;
}
};
Via Active questions tagged javascript - Stack Overflow https://ift.tt/Jk789W3
Comments
Post a Comment