-1

I'm trying to calculate a string based on user input and show some message:

$('#myInput').keyup(function() {
    if(this.value.length<=158)
     $('#charCount').text('We will deduct 1 credit from your account');
    else if(this.value.length>158 || this.value.length<316)
         $('#charCount').text('We will deduct 2 credit from your account');
    else if(this.value.length>316 || this.value.length<400)
        $('#charCount').text('We will deduct 3 credit from your account');
});

http://jsfiddle.net/p9jVB/11/

The problem is the last else if is not working... Have I missed something?

1
  • 3
    Other problem is that if you have a text which length's 316 it won't enter in any if statement. Commented Dec 6, 2012 at 9:52

2 Answers 2

7

It looks like you mean &&, not ||

$('#myInput').keyup(function() {
    if(this.value.length<=158)
         $('#charCount').text('We will deduct 1 credit from your account');
    else if(this.value.length>158 && this.value.length<=316)
         $('#charCount').text('We will deduct 2 credit from your account');
    else if(this.value.length>316 && this.value.length<400)
        $('#charCount').text('We will deduct 3 credit from your account');
});
Sign up to request clarification or add additional context in comments.

Comments

4
else if(this.value.length>158 || this.value.length<316)
    $('#charCount').text('We will deduct 2 credit from your account');
else if(this.value.length>316 || this.value.length<400)
    $('#charCount').text('We will deduct 3 credit from your account');

Every number in the world is greater than 158 or less than 316, so the alternative condition will never be reached.

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.