Shashank Bhatt
Shashank Bhatt

Reputation: 857

How to check if input of all types is empty or not using jquery?

I would like to check if input type is empty or not for all types.I am having following code.

Now the problem with jquery is that it works for text based input but when I add characters in number field then length method doesn't work as intended.

I would like to check for non-empty fields for all types of input.If user has entered anything(text or number or whatever) in the field, it should be considered as non-empty.

$(".textyformcontrol").blur(function() {

  if ($(this).val().length > 0) {
    $(this).parent('.formcontrolparent').find('.animatedlabel').addClass('filled');
  } else {
    $(this).parent('.formcontrolparent').find('.animatedlabel').removeClass('filled');

  }


});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="formcontrolparent form-group">
  <input required type="number" name="Phone no" class="form-control textyformcontrol" />
  <label class="animatedlabel">Phone no.</label>
  <img src="icons/mobile_16.png" alt="phone number" />
</div>

Upvotes: 2

Views: 1149

Answers (2)

aks7
aks7

Reputation: 441

Try this :

$(document).on("blur" ,".textyformcontrol" ,function() {

 if ($(this).val() != "") {  // Non-empty 
     // Do something 

  } else {  // empty
     // Do something
  }

});

Upvotes: 1

Mayank Pandeyz
Mayank Pandeyz

Reputation: 26258

Try this:

$("input:empty").length == 0;

If its value is zero, means every input is having some value.

Upvotes: 0

Related Questions