Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if ALL form inputs are empty with jQuery

Tags:

I'm trying to validate a contact form and I want to create some sort of 'form completed' message once every input field has been filled in (some of the inputs are text boxes, some are radio buttons).

Here's my code so far:

$(document).ready(function() {    $('.form:input').each(function() {      if ($(this).val() != "") {        $('.congrats').css("display", "block");      }    });  });
p.congrats {    display: none;  }
<div class="form">    <input type="text" />    <br />    <input type="text" />  </div>  <p class="congrats">Congrats!</p>

http://jsfiddle.net/7huEr/

like image 349
mmmoustache Avatar asked May 09 '12 13:05

mmmoustache


People also ask

How check form is empty or not in jQuery?

each(function() { if ($(this). val() != "") { empty = false; return false; } }); This should look all the input and set the empty var to false, if at least one is not empty.

Is Empty jQuery input?

You can use the val() method to test or check if inputs are empty in jQuery. The following example will add a red outline around the inputs if it is focused but not filled out.

How check multiple textbox is empty or not in jQuery?

click(function(e) { var isValid = true; $('input[type="text"]'). each(function() { if ($. trim($(this). val()) == '') { isValid = false; $(this).


2 Answers

This should get you started:

$(document).ready(function() {      $(".form > :input").keyup(function() {          var $emptyFields = $('.form :input').filter(function() {              return $.trim(this.value) === "";          });            if (!$emptyFields.length) {              console.log("form has been filled");          }      });  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <div class="form">      <input type="text" /><br />      <input type="text" />  </div>  <p class="congrats"></p>
like image 73
karim79 Avatar answered Sep 28 '22 06:09

karim79


try this :

$("#a").on('click',function () {  var bad=0;   $('.form :text').each(function(){           if( $.trim($(this).val()) == "" ) bad++;                                 });            if (bad>0) $('.congrats').css("display","block").text(bad+' missing');       else $('.congrats').hide();   });                 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <div class="form">      <input type="text" /><br />      <input type="text" />  </div>  <p class="congrats"></p><input style="width:100px" value="check" id="a" type="button" />
like image 20
Royi Namir Avatar answered Sep 28 '22 07:09

Royi Namir