2

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>

4
  • Don't go for length, just check if the value is '' or not. Commented Mar 2, 2017 at 6:08
  • If ($(this).val()) works for all inputs expect number when I add characters in number field Commented Mar 2, 2017 at 6:18
  • check this fiddle, even the length seems to give value. jsfiddle.net/rd6pp7ns Commented Mar 2, 2017 at 6:19
  • Not worked for input type number when I add characters in number field Commented Mar 2, 2017 at 6:28

2 Answers 2

1

Try this :

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

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

  } else {  // empty
     // Do something
  }

});

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

Comments

0

Try this:

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

If its value is zero, means every input is having some 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.