使用javascript在文本框中允许Ctrl + C,Ctrl + X和Ctrl + V组合(Allow Ctrl+C, Ctrl+X and Ctrl+V combinations in text

编程入门 行业动态 更新时间:2024-10-25 10:29:12
使用javascript在文本框中允许Ctrl + C,Ctrl + X和Ctrl + V组合(Allow Ctrl+C, Ctrl+X and Ctrl+V combinations in textbox using javascript)

我创建了一个webform,我想验证输入。 用户只能输入数字(包括Ctrl + CCtrl + XCtrl + V组合)。

以下是我的javascript代码。

var unicode = e.charCode ? e.charCode : e.keyCode if (unicode != 8 && unicode != 9 && unicode != 46 && unicode != 37 && unicode != 39) { //if the key isn't the backspace key (which we should allow) if (unicode < 48 || unicode > 57) //if not a number return false //disable key press }

但它没有验证(“。”)句点作为删除的关键代码,并且Ctrl + CCtrl + XCtrl V的组合不起作用。

有人可以帮忙吗?

I have created a webform where I want to validate the input. User can only input numbers (including Ctrl+C, Ctrl+X and Ctrl+V combinations).

Below is my javascript code.

var unicode = e.charCode ? e.charCode : e.keyCode if (unicode != 8 && unicode != 9 && unicode != 46 && unicode != 37 && unicode != 39) { //if the key isn't the backspace key (which we should allow) if (unicode < 48 || unicode > 57) //if not a number return false //disable key press }

But it is not validating (".") period as the key code for delete and the combinations of the Ctrl+C, Ctrl+X and CtrlV are not working.

Can anyone help?

最满意答案

我找到了以下解决方案:

function numbersonly(evt) { evt = (evt) ? evt : window.event; var charCode = (evt.which) ? evt.which : evt.keyCode; if (charCode > 31 && (charCode < 48 || charCode > 57)) { return false; } return true; }

谢谢大家的时间和帮助。

I found the solution below:

function numbersonly(evt) { evt = (evt) ? evt : window.event; var charCode = (evt.which) ? evt.which : evt.keyCode; if (charCode > 31 && (charCode < 48 || charCode > 57)) { return false; } return true; }

Thank you all for time and help.

更多推荐

本文发布于:2023-08-01 11:16:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1357733.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组合   框中   文本   javascript   textbox

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!