I want to prevent form submission when there is no input in the text boxes but the form submits even when the input is empty. How do I fix this? <input type="text" size="30" id="zip" name="zip"><br /> <input type="text" size="30" id="phone" name="phone"> var input = $("#zip").val(); var input1 = $("#phone").val(); if ( $.trim(input).length == 0 || $.trim(input1).length == 0 ){ event.preventDefault(); } Code (JavaScript): <button type="submit" class="btn btn-success">Continue</button>
It's because you haven't wrapped it in a function-call, and "event" is not defined. This works: $('button[type=submit]').click(function(event) { var input = $("#zip").val(); var input1 = $("#phone").val(); console.log(input.length); if ( $.trim(input).length == 0 || $.trim(input1).length == 0 ){ event.preventDefault(); } }) Code (JavaScript):