简体   繁体   English

防止将错误的值粘贴到输入数字类型中

[英]Prevent pasting wrong values into Input number type

I have an Input element with type = "number", which is supposed to only accept 0,1,..,9 and "." 我有一个类型为“ number”的Input元素,应该只接受0,1,..,9和“。”。 Per default you can also wrie +, - ans soem other characters which I prevent with EventListener "keydown" and keyCodes. 默认情况下,您也可以使用+,-和其他字符,这些字符我可以通过EventListener“ keydown”和keyCodes来防止。 It works fine. 工作正常。

But how can I prevent the user from pasting or drag and dropping some other stuff into the input? 但是,如何防止用户将其他内容粘贴或拖放到输入中呢? I tried to validate input.value but it actually gives me "" when it is not a valid number. 我试图验证input.value,但是当它不是有效数字时,它实际上给了我“”。 Is there a way to see what the user wants to paste, before it is pasted? 在粘贴之前,是否有办法查看用户要粘贴的内容?

I also know, that I could change the type to "text", but at leas for now it is not an option. 我也知道,我可以将类型更改为“文本”,但目前至少不是一种选择。

Thanks. 谢谢。

If you want to reject any input other than 0-9 and . 如果要拒绝0-9和以外的任何输入. , try this: , 尝试这个:

 Array.from(document.getElementsByClassName('restricted')).forEach(elem => { elem.addEventListener('input', event => { event.target.value = event.target.value.replace(/[^0-9\\.]/g, ''); }); }); 
 <input type="text" class="restricted"> 

You can add .restricted to any input to achieve the same effect once this script is in place. 您可以在任何输入后添加.restricted以实现相同的效果。 Unfortunately, this trick won't work with input[type="number"] , because event.target.value is '' whenever an invalid input state is detected. 不幸的是,这招不会与工作input[type="number"]因为event.target.value''每当检测到一个无效的输入状态。

Without javascript 没有JavaScript

 :invalid { color: red } 
 <form method="post" action="https://httpbin.org/post"> <input type="number" name="num" pattern="[0-9]*[.,]?[0-9]+" min="0" step="any"> <!-- you won't be able to submit when the form is invalid --> <input type="submit"> </form> 

PS (browser take care of normalizing the , to . ) PS(浏览器采取正火的照顾,.

try this event: 试试这个事件:

Pasting: 粘贴:

https://developer.mozilla.org/en-US/docs/Web/Events/paste https://developer.mozilla.org/en-US/docs/Web/Events/paste

Draging: Draging:

https://developer.mozilla.org/en-US/docs/Web/Events/drag https://developer.mozilla.org/en-US/docs/Web/Events/drag

maybe look at this: 也许看看这个:

How do I make control characters programatically in Javascript? 如何在Javascript中以编程方式制作控制字符?

EDIT: 编辑:

document.getElementById('id').addEventListener('paste', (e) => {
  let pre = e.target.value;
  window.setTimeout(() => {
    console.log(pre);
    console.log(e.target.value);
    /*
    Get the pasted value here, and edit the input
    */
  });
});

Use this: 用这个:

It allows you to use any kind of input filter on a text , including various numeric filters. 它允许您在文本上使用任何类型的输入过滤器,包括各种数字过滤器。 This will correctly handle Copy+Paste, Drag+Drop, keyboard shortcuts, context menu operations, non-typeable keys, and all keyboard layouts. 这将正确处理“复制+粘贴”,“拖动+拖放”,键盘快捷键,上下文菜单操作,不可键入的键以及所有键盘布局。

 function isNumberKey(evt){ var charCode = (evt.which) ? evt.which : event.keyCode if (charCode > 31 && (charCode < 48 || charCode > 57)) return false; return true; } 
 <input name="someid" onkeypress="return isNumberKey(event)"/> 

I have countered something similar, i used regex to filter the entered value of any characters only accepts numbers. 我已经提出过类似的建议,我使用了正则表达式来过滤任何字符的输入值,只接受数字。 the down side to this solution is that it only accept integer numbers not numeric. 该解决方案的缺点是它仅接受整数,而不接受数字。 here is a code example: 这是一个代码示例:

handleChange = (e) => {
    const re = /^[0-9\b]+$/;
    if (e.target.value === '' || re.test(e.target.value)) {
         this.setState({Number: e.target.value})
      }
  }

I'm using reactjs for this example, but of course you can adjust the code to serve your purpose 我在此示例中使用reactjs,但是您当然可以调整代码以实现您的目的

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

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