1

I have this short example

<input value="2" min="0.1" max="1000" type="number" />

The input with: min=0.1 max=1000 value=2 step=1 The problem is: If I click on up arrow (pseudo-element for number type=number) - the input value becames 2.1 and then 3.1 WHY does it add 0.1 on the first click? And if you change 3.1 to 3 and click up arrow you still see 3.1 and 4.1 on the second click

3
  • It works for float min number only. If min is 0 or something else (int number) everything works fine Commented Nov 16, 2021 at 14:12
  • developer.mozilla.org/en-US/docs/Web/HTML/Attributes/…: "The value of min and step define what are valid values" Your minimum is specified as 0.1, and only values that satisfy min + x * step are considered valid (with x being an integer greater-equal 0.) Commented Nov 16, 2021 at 14:18
  • @CBroe thank you Commented Nov 17, 2021 at 16:34

2 Answers 2

1

Adding step attribute solves the problem

<input value="2" min="0.1" max="1000" step="0.1" type="number" />

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

1 Comment

But I need step="1". And in this way it does not work
0

You can use js like below.

$(this).off('input').on('input',function(e){
  var value = $(this).val();
  var regExp = /^\d{0,10}(\.\d{0,1})?$/;
  if(!regExp.test(this.value)){
    $(this).val(value.substring(0,value.length-1));
  }
});

You can also control length of input or max/min for float. Don't forget to change this to your input tag name. Good luck! :)

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.