javascript check if input field is empty code example
Example 1: if input value is null do something
if(document.getElementById("question").value.length == 0)
{
alert("empty")
}
Example 2: javascript form submit on button click check if required fields not empty
const checkEmpty = document.querySelector('#checkIt');
checkEmpty.addEventListener('input', function () {
if (checkEmpty.value &&
checkEmpty.value.length > 0 &&
checkEmpty.value.trim().length > 0
)
{ console.log('value is: '+checkEmpty.value);}
else {console.log('No value');
}
});
Example 3: how to check if all inputs are not empty with javascript
const inputFeilds = document.querySelectorAll("input");
const validInputs = Array.from(inputFeilds).filter( input => input.value !== "");
console.log(validInputs)