0

i have several input fields with a class name of required.

i have a code below to check if all my <input> fields have a value, if not empty or null, it will show/unhide a specific div.

but it doest seem to work on me.

also, by default, #print is displayed as none via CSS.

<!-- index.php -->

<input type="text" id="getID" class="required form-control" placeholder="id">
<input type="text" id="getName" class="required form-control" placeholder="name">

<!-- script.js -->

$(document).ready(function() { 
  $('input.required').each(function() { 
    if($(this).val() != "") {
        $("#print").show();
    }
    else {
        $("#print").hide();
    }
  });
});
1
  • 2
    You haven't given the inputs any value and the validation is running on $(document).load(), you could try binding it to a click or a submit event instead Commented Sep 30, 2013 at 15:48

3 Answers 3

2

I'd suggest:

$('#print').toggle(!($('input.required').length == $('input.required').filter(function () {
        return this.value;
    }).length));

Simplified JS Fiddle demo.

Obviously this should be run on submit, assuming you want to only show the #print element as a validation prior to submission.

References:

Sign up to request clarification or add additional context in comments.

Comments

1

As I stated in my comment above, you're checking the values when the page loads, before the user has any chance to enter anything. If you have a button on your page, bind the event to that which will fire the function at the right time.

Something a little like this jFiddle

index.php

<input type="text" id="getID" class="required form-control" placeholder="id">
<input type="text" id="getName" class="required form-control" placeholder="name">
<div id="button">Submit</div>
<div id="print">You're missing something D:!</div>

script.js

$('#button').on('click', function() { 
    $('#print').hide();
    var error=false;
    $('input.required').each(function() { 
        if($(this).val() == "") {
            error=true;
        }
    });
    if(error) {
        $('#print').show();   
    }
});

2 Comments

But the check is inside an each so the #print div will be visible according to the last input validation
Edited, noticed that the second I put it into a fiddle :P
0

Try

$(document).ready(function () {
    //the default state
    var valid = true;
    $('input.required').each(function () {
        //if the value of the current input is blank then the set is invalid and we can stop the iteration now
        if ($.trim(this.value) == '') {
            valid = false;
            return false;
        }
    });
    //set the visibility of the element based on the value of valid state
    $("#print").toggle(!valid);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.