jquery check if input is empty on submit 1

jquery check if input is empty on submit

var empty = true;
$('input[type="text"]').each(function() {
   if ($(this).val() != "") {
      empty = false;
      return false;
   }
});

Here is what the above code is Doing:
1. It’s selecting all the input elements with type=”text”
2. It’s looping through each of them
3. It’s checking if the value of the input element is empty
4. If it’s empty, it sets the empty variable to true
5. If it’s not empty, it sets the empty variable to false and breaks out of the loop
6. If the empty variable is true, it displays the message

Similar Posts