1

Html

<input type="text" name="test[]" class="test">

How can I check every input has value? This is what I tried but if one the input has value it will not alert the message

Script

var x = new Array();
var message = "";

jQuery("input[name='test[]']").each(function(){
  var y = jQuery(this).val().toLowerCase();
  if(y){
    x.push(y);
  }
});

if(x.length === 0){
  message += "missing";
};

if(message != ""){
  alert(message);
}

EDIT I want to alert if one of input has no value

6
  • did you want it to alert if the input has a value? or what did you want to happen? Commented Mar 6, 2017 at 2:26
  • @Rick I want to alert it no value if one of input is no value Commented Mar 6, 2017 at 2:31
  • seems to work fine here.. Commented Mar 6, 2017 at 2:35
  • @Rick yes but it doesnt alert if one of input has value Commented Mar 6, 2017 at 2:42
  • ah well you only had one input so I guess I didn't catch on to what you were asking.. Commented Mar 6, 2017 at 2:47

1 Answer 1

2

This will alert 'no value' if one of the inputs has no value. Just check the $.val() and alert and break the loop if it doesn't return anything.

$('button').on('click', function() {

  $("input[name='test[]']").each(function() {
    if (!$(this).val()) {
      alert('no value');
      return false;
    }
  });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" name="test[]" class="test">
<input type="text" name="test[]" class="test">
<input type="text" name="test[]" class="test">
<button>validate</button>

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

1 Comment

@PureRhymerOrganization awesome, no problem: )

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.