How to prevent HTML form submission when input field are empty in JavaScript

1 Answer

0 votes
 <form id='search-form' method='get' value action="/search.php">
     <input id='search-text' name="query" type='text' />
     <button id='search-button' type='submit' onclick="return formIsNotEmpty()"></button>
</form>
function formIsNotEmpty() {
    if (document.getElementById('search-form').elements.item(0).value == "") {
           return false;
    }
    return true;
}

 



answered Jun 12, 2018 by avibootz
edited Jun 12, 2018 by avibootz
...