使用javascript在文本框中允许Ctrl + C,Ctrl + X和Ctrl + V组合(Allow Ctrl+C, Ctrl+X and Ctrl+V combinations in textbox using javascript)
我创建了一个webform,我想验证输入。 用户只能输入数字(包括Ctrl + C , Ctrl + X和Ctrl + 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 + C , Ctrl + X和Ctrl 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.
更多推荐
发布评论