Here you can make another way, which also works if someone presses the enter button in a text field or if the browser automatically completed the input:
$(function() { $("form button").attr("disabled", true); //disable buttons at first var inputs = 0; //keep count of the inputs $("form input, form select, form textarea").each(function() { inputs++; //increment counter $(this).keypress(function() { if ($.trim($(this).val()) != "") { //if the value isnt empty inputs--; //decrement counter if (inputs == 0) { //if theyre all filled in $("form button").attr("disabled", false); } } }).trigger("keypress"); //in case it was autofilled by the browser }); $("form").submit(function(e) { //prevent the form being submitted by pressing enter if (inputs != 0) { //if they arent all filled in alert("Some fields aren't filled in. Please fix them and try again."); //tell the user e.preventDefault(); //cancel sending the form return false; } }); });
source share