1

I got this HTML:

<input type="number" name="eingangstuer_hoehe" min="70" max="200">

And this is the jQuery/javascript:

$("input").change(function(e) {
        var $this = $(this);
        var val = $this.val();
        var max = $this.attr("max");
        var min = $this.attr("min");


        if(val > min || val < max)
        {

        }
        else
        {
            if (val > max){
                e.preventDefault();
                $this.val(max);
            }

            if (val < min)
            {
                e.preventDefault();
                $this.val(min);
            }
        }
});

Typing 1 or 2 will not change anything. Typing 3 - 69 will work how it supposed to. Typing everything above 199 (yes also 200) will change the value to 70..

It also won't change the values above 600

2 Answers 2

2

You should use the condition as shown in the code snippet below.

Also You're compering string and not numbers. try parsing the values to integer with parseInt

your code should look like this:

    $("input").change(function(e) {
        var $this = $(this);
        var val = parseInt($this.val());
        var max = parseInt($this.attr("max"));
        var min = parseInt($this.attr("min"));

        if(val < min || val > max){
            e.preventDefault();
            $this.val(max);
        }
    }

});

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

Comments

0

Wrong condition found. Change if(val > min || val < max) to if(val >= min && val <= max)

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.