简体   繁体   English

如何使用javascript或html设置十进制输入范围?

[英]How to set decimal input range using javascript or html?

I have an input and I need it to be limited to only these values:我有一个输入,我需要它仅限于这些值:

Min: 0.01 max 999.99最小:0.01 最大 999.99

If I use maxlength="6" I can enter, for instance, 999.99 but also 1000.1 which is not an accepted value.例如,如果我使用maxlength="6" ,我可以输入999.99 ,但也可以输入1000.1 ,这不是可接受的值。

What I've tried:我试过的:

1st attempt (not working):第一次尝试(不工作):

<input type="number" id="quantity" name="quantity" maxlength="6" min="0.01" max="999.99">

2nd attempt (partially working):第二次尝试(部分工作):

 let maximumFractionDigits = 2; const formatNumber = (value) => { return parseFloat(value.replace(/,/g,'')).toLocaleString('en-US', { maximumFractionDigits }); } $('input[name="test"]').on('keyup',function(){ maximumFractionDigits = 2; if (.$(this).val() || (maximumFractionDigits && $(this).val().endsWith('.'))) { return } $(this).val(formatNumber($(this).val()),replace(/,/g;'')); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> INPUT TEST <input type="text" maxlength="6" name="test" class="form-control"> <br>

With the second approach, I still can input weird numbers like 10000. and values like 0.0X can't be introduced (I guess it's rounded), how could I just limit the values to Min: 0.01 max 999.99 ?使用第二种方法,我仍然可以输入像10000.这样的奇怪数字。并且不能引入像0.0X这样的值(我猜它是四舍五入的),我怎么能将值限制为Min: 0.01 max 999.99

You can check for 3 conditions, For first 2 convert number to string您可以检查 3 个条件,前 2 个将数字转换为字符串

  1. The max length of the string should be 6 maxlength = 6字符串的最大长度应为 6 maxlength = 6

  2. If string has decimal point then.split('.') and check the length of decimal numbers const decimal = input.split('.')[1]如果字符串有小数点 then.split('.') 并检查十进制数字的长度const decimal = input.split('.')[1]

  3. Check if the number is 0.01 < number < 999.99检查数字是否为0.01 < number < 999.99

You have to methods to achieve the same你必须有方法来实现相同的

  1. change the input type, and set min-max values:更改输入类型,并设置最小-最大值:
<input type="number" min=0.01 max=999.99 name="test" class="form-control" aria-label="Equipment Procurement %">
  1. If you don't want to change the type:如果您不想更改类型:
const numInput = document.querySelector(".form-control");
numInput.addEventListener("input", (event) => {
  const number = parseInt(e.target.value);
  if (number > 999.99 && number < 0.01) // throw Error or do whatever you want to do
})

So basically I found a solution:所以基本上我找到了一个解决方案:

 const input = document.querySelector('.example'); input.addEventListener('input', updateValue); function updateValue(e) { let number = e.target.value; number = number.toString(); if(number.match(/^\d{1,3}(\.\d{1,2})?$/)){ console.log("True"); } else{ const newNum = (e.target.value).toString().slice(0, -1) e.target.value = parseFloat(newNum); } }
 <input type="number" class="form-control example" id="quantity" name="quantity" maxlength="6" min=0.01 max=999.99>

Hope this helps if someone has the same situation如果有人有同样的情况,希望这会有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM