0

my jQuery if condition does not work like how it should. Everything works good but not on input number "2,3,4,5,6,7,8,9". Below is the code. Wonder what went wrong.

$(function(){
        $('input').blur(function(){
            var LowerLimit = 10;
            var UpperLimit = 70;
            var CurrVal = $(this).val();
            if (CurrVal<LowerLimit) {
                $(this).val(LowerLimit);
            }
            if (CurrVal>UpperLimit) {
                $(this).val(UpperLimit );
            }
        });
    });
3
  • 1
    Parse your input to int and try again Commented Jan 16, 2018 at 9:21
  • Or change your input type text to number in HTML codes. Commented Jan 16, 2018 at 9:22
  • You should parseInt() our input value of use type="number" in your input box. Commented Jan 16, 2018 at 9:26

2 Answers 2

1

try wrapping into parseInt() like this

var CurrVal = parseInt($(this).val());
Sign up to request clarification or add additional context in comments.

Comments

0

See your desired output

$(function(){
        $('input').blur(function(){
            var LowerLimit = 10;
            var UpperLimit = 70;
            var CurrVal = $(this).val();
            if (CurrVal<LowerLimit) {
                $(this).val(LowerLimit);
            }
            if (CurrVal>UpperLimit) {
                $(this).val(UpperLimit );
            }
        });
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="number" />

2 Comments

What if OP wants input type="text" only.
then use parseInt($(input).val())

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.