13

Here I have many input text fields with same class like

<input type="text" class="MyClass"></input>
<input type="text" class="MyClass"></input>
<input type="text" class="MyClass"></input>

My requirement is to check wheather all input fields of this class is empty or not. I've tried this

if(!('.MyClass').val()){
alert("empty");
}

But it doesn't make any result. Can anyone help?

1
  • @Mr_Green $('.MyClass') will return array of items... Commented Feb 13, 2015 at 6:57

9 Answers 9

22

You can check

$('button').click(function() {
  var $nonempty = $('.MyClass').filter(function() {
    return this.value != ''
  });

  if ($nonempty.length == 0) {
    alert('empty')
  }
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="text" class="MyClass" />
<input type="text" class="MyClass" />
<input type="text" class="MyClass" />
<button>test</button>

Or using a flag

$('button').click(function() {
  var flag = false;
  $('.MyClass').filter(function() {
    if (this.value != '') {
      flag = true;
      //no need to iterate further
      return false;
    }
  });

  if (!flag) {
    alert('empty')
  }
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="text" class="MyClass" />
<input type="text" class="MyClass" />
<input type="text" class="MyClass" />
<button>test</button>

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

3 Comments

This only works for the first input field. If you put something in one of them and leave the rest empty, it still thinks they're all filled.
@Xander that is what the OP asked for -- a way to test whether all fields are empty, not a way to test whether all fields are filled in.
remove the $ sign from variables in the first method.
3

You can compare the length of number of input with number of empty input using :

var allemptylength=$(".MyClass").filter(function() {
    return this.value.length !== 0;
})});

if($('.MyClass').length== allemptylength.length){
  alert("all empty");
}

Working Demo

2 Comments

The input element can't have children so that will be always empty
It will not work, as :empty checks content of the element, not its value.
1

To make sure each classes are checked, use

$.each($('.MyClass'),function() {
   if ($(this).val().length == 0) {
    alert('empty');
   }
});

Comments

1

You can check it like this:

var isEmpty = !$('.MyClass').filter(function() {
    return this.value.trim();
}).length;

Also remove all </input> tags. input elements are self-closable.

Comments

0

try this

    var hasNoValue;
    $('.MyClass').each(function(i) {
        if ($(this).val() == '') {
              hasNoValue = true;
        }
    });
    if (hasNoValue) {
        alert('All have no value');
    }

Comments

0

try is(':checked')

$('.MyClass').each(function(){
    alert($(this).is(':checked');)
});

this will alert only the checked elements.

Comments

0
$('input').each(function(index,value){
    if($(this).val()!=''){
        $(this).addClass('success');
    }else{
        $(this).removeClass('someClass');
        $(this).addClass('warning');
        $(this).css('border', '1px solid red');
        //do something else...
    }
});

Comments

0

Try this . 100% working..

var emptyLength = $(".MyClass").filter(function() {
    return this.value == "";
}).length;
 if (emptyLength > 0)
{
    alert("Please enter all peramerter's value");
    return;
}

Thanks.

Comments

0

Try this. It works for me

var flag = 1;
$(".MyClass").each(function(i){
    if ($(this).val() == "")
        flag++;
});
if (flag == 1)
    alert('all have values');
else
    alert('some or all doesn\'t have value'); 

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.