简体   繁体   English

防止android webview中的无效输入值更改

[英]Prevent invalid input value change in android webview

I'm trying to prevent the change in input value for two cases.我试图防止两种情况下输入值的变化。

If there is如果有

  1. multiple negative sign "-" eg.多个负号“-”例如。 prevent typing of "-1-2-3"防止输入“-1-2-3”
  2. multiple "."多个“.” value eg.价值,例如。 prevent typing of "1.2.3"防止输入“1.2.3”

For desktop browser, I'm able to achieve using the following functions.对于桌面浏览器,我可以使用以下功能来实现。

 function valueEntered(element, event){ var newVal = element.value if(event.code==="Minus") { if(element.value.length==0) return true // allow minus at start element.value = -1*parseFloat(newVal) // change the sign of number return false } else if(event.code==="Period") { if(element.value.indexOf('.')>-1) return false } return true }
 <input type="number" style="border-radius: 5px;" onkeypress="return valueEntered(this, event)" />

It's working perfectly fine.它工作得很好。 But if I use this snippet in android webview, it's not preventing as it should.但是如果我在 android webview 中使用这个片段,它并没有像它应该的那样阻止。

I trusted https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/code which tells that event.code will work for android webview also, but it doesn't.我信任https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/code ,它告诉event.code也适用于 android webview,但事实并非如此。

Also event.key supports detecting numeric values only. event.key也仅支持检测数值。 If I type - , .如果我输入- , . , , ; , , ; the property event.key returns Unidentified .属性event.key返回Unidentified

Similarly, the property event.keyCode returns 229 for all keypress stated above.类似地,对于上述所有按键,属性event.keyCode返回229

So it is not actually possible to achieve what I want currently in android webview.所以实际上不可能在android webview中实现我目前想要的。

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

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