简体   繁体   English

使用JavaScript在文本框中允许Ctrl + C,Ctrl + X和Ctrl + V组合

[英]Allow Ctrl+C, Ctrl+X and Ctrl+V combinations in textbox using javascript

I have created a webform where I want to validate the input. 我创建了一个Webform,我想在其中验证输入。 User can only input numbers (including Ctrl + C , Ctrl + X and Ctrl + V combinations). 用户只能输入数字(包括Ctrl + CCtrl + XCtrl + V组合)。

Below is my javascript code. 以下是我的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
    }

But it is not validating (".") period as the key code for delete and the combinations of the Ctrl + C , Ctrl + X and Ctrl V are not working. 但这不能验证(“。”)期间作为删除的关键代码,并且Ctrl + CCtrl + XCtrl V的组合无效。

Can anyone help? 有人可以帮忙吗?

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. 谢谢大家的时间和帮助。

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

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