2

I am trying to keep an input disabled until 2 inputs have atleast 4 characters. The closest I was able to find on SO was a user settling for 8 characters total but this would allow for one of the inputs to be blank in the other had over 8. Here is what I have tried:

Without on()

$('#ancestor input').keyup(function(){
  var foo = $('input[name=foo]').val();
  var bar = $('input[name=bar]').val();    
    if(foo.length > 4 && bar.length > 4){  
      $('#some-button').prop('disabled', false);
      }else{
      $('#some-button').prop('disabled', true);
      }  
});

With on()

$('#ancestor input').on('keyup',function(){
  var foo = $('input[name=foo]').val();
  var bar = $('input[name=bar]').val();    
    if(foo.length > 4 && bar.length > 4){  
      $('#some-button').prop('disabled', false);
      }else{
      $('#some-button').prop('disabled', true);
      }  
});

Interestingly I can console.log(foo.length) and console.log(bar.length) and see both lengths ticking up just fine with each keyup.

5
  • 1
    JSFiddle please, any problems you have? Commented Nov 19, 2015 at 17:31
  • 2
    At least 4 characters so should be >= 4. Now, what is your issue??? Commented Nov 19, 2015 at 17:33
  • 1
    "atleast 4 characters" -> you should've used >3 instead of >4? jsfiddle.net/9woyctz2 Commented Nov 19, 2015 at 17:33
  • Except for the typo in the > sign, it looks like your stuff was working. Commented Nov 19, 2015 at 17:40
  • your code might works, you should use >3 for atleast four chars Commented Nov 19, 2015 at 17:42

4 Answers 4

5

As A. Wolff has mentioned, your condition is using > instead of >=.

if (foo.length >= 4 && bar.length >= 4) {  

If you have 2 input elements, you can also invert the condition:

$('#ancestor input').keyup(function(){
  var lengthA = $('input[name=foo]').val().length;
  var lengthB = $('input[name=bar]').val().length;

  $('#some-button').prop('disabled', lengthA < 4 || lengthB < 4);
});

Conceptually that means "disable if any input is not 4 characters long" instead of "do not disable if both inputs are more than 4 characters long".

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

2 Comments

@nicael reverted your change, the opposite of not( A >= 4 && B >= 4) is A < 4 || B < 4. Is it fine to just edit it back? Don't know yet how to provide feedback "stackoverflow-correctly".
It's completely fine, because I have been just dumb. Sorry.
2

Your code works as far as I can see. The only issue is that your condition checks for at least 5 characters, not 4 characters, due to the operator you've chosen to use.

Use a >= instead, as this asserts something that is "equal to or greater than".

$('#ancestor input').on('keyup',function(){
  
  var foo = $('input[name=foo]').val();
  var bar = $('input[name=bar]').val(); 
  
  $('#some-button').prop('disabled', !(foo.length >= 4 && bar.length >= 4));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<form id="ancestor">
  <input name="foo">
  <input name="bar">
  <input id="some-button" type="submit" disabled>
</form>

2 Comments

Might want to have it disabled initially.
@Ernesto just noticed that. :-)
0

You could try matching the jquery object length against a filtered jquery object to see if they are different.

$('#ancestor input').on('input keyup change paste', function () {
    var els = $('input[name=foo],input[name=bar]');
    var filtered = els.filter(function(){
       return (this.value.length >= 4); 
    });
    $('#some-button').prop('disabled', (els.length != filtered.length));
});

http://jsfiddle.net/9se4x2an/

Comments

0

The fact is, I've been staring at this for 3 hours and it was just a typo (misspelled length as lengh on one of the variables). Needed more coffee to see it apparently. Thanks to everyone for helping.

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.