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/
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.
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.
click(function(e) { var isValid = true; $('input[type="text"]'). each(function() { if ($. trim($(this). val()) == '') { isValid = false; $(this).
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>
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" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With