在javascript中使用键盘快捷键验证
我想验证我在按键上的输入 我的Java代码是这样的:在javascript中使用键盘快捷键验证,javascript,validation,Javascript,Validation,我想验证我在按键上的输入 我的Java代码是这样的: function numberValidate(evt) { var theEvent = evt || window.event; var key = theEvent.keyCode || theEvent.which; var key1 = key; key = String.fromCharCode( key ); var regex = /^[0-9\u0008\u000
function numberValidate(evt) {
var theEvent = evt || window.event;
var key = theEvent.keyCode || theEvent.which;
var key1 = key;
key = String.fromCharCode( key );
var regex = /^[0-9\u0008\u0009\u0016]/;
if(key1 == 46 || key1 == 8 || key1 == 9 || key1 == 16 || key1 == 37 || key1 == 39 || key1 == 17 || key1 == 36 || key1 == 35)
return false;
if( !regex.test(key) ) {
theEvent.returnValue = false;
if(theEvent.preventDefault) theEvent.preventDefault();
}
}
function textValidate(evt) {
var theEvent = evt || window.event;
var key = theEvent.keyCode || theEvent.which;
var key1 = key;
key = String.fromCharCode( key );
var regex = /^[0-9a-zA-Z\u0600-\u06FF\u0008\u0009\u0016\. -]/;
if(key1 == 46 || key1 == 8 || key1 == 9 || key1 == 16 || key1 == 37 || key1 == 39 || key1 == 17 || key1 == 36 || key1 == 35)
return false;
if( !regex.test(key)) {
theEvent.returnValue = false;
if(theEvent.preventDefault) theEvent.preventDefault();
}
}
字母、数字、空格、句号和阿拉伯字母如下:
function numberValidate(evt) {
var theEvent = evt || window.event;
var key = theEvent.keyCode || theEvent.which;
var key1 = key;
key = String.fromCharCode( key );
var regex = /^[0-9\u0008\u0009\u0016]/;
if(key1 == 46 || key1 == 8 || key1 == 9 || key1 == 16 || key1 == 37 || key1 == 39 || key1 == 17 || key1 == 36 || key1 == 35)
return false;
if( !regex.test(key) ) {
theEvent.returnValue = false;
if(theEvent.preventDefault) theEvent.preventDefault();
}
}
function textValidate(evt) {
var theEvent = evt || window.event;
var key = theEvent.keyCode || theEvent.which;
var key1 = key;
key = String.fromCharCode( key );
var regex = /^[0-9a-zA-Z\u0600-\u06FF\u0008\u0009\u0016\. -]/;
if(key1 == 46 || key1 == 8 || key1 == 9 || key1 == 16 || key1 == 37 || key1 == 39 || key1 == 17 || key1 == 36 || key1 == 35)
return false;
if( !regex.test(key)) {
theEvent.returnValue = false;
if(theEvent.preventDefault) theEvent.preventDefault();
}
}
现在,我的问题是:
我是对的,还是我必须更改代码的某些部分,为什么
我想在数字部分使用CTRL+A或CTRL+C或CTRL+V,但由于我不允许使用字母,所以它不允许我在Firefox等浏览器中使用字母
取出公共位作为另一个函数,这样您就可以添加测试,而不会有太多的代码重复
function commonPermitted(e) {
var arr = [
8, // backspace
9, // tab
16, // shift
17, // ctrl
35, // end
36, // home
37, // left
39, // right
46, // del
// you may want to permit enter/return here too
];
if (arr.indexOf(e.keyCode) !== -1)
return true;
if (e.ctrlKey && ['a', 'c', 'v', 'x'].indexOf(String.fromCharCode(e.keyCode).toLowerCase()) !== -1)
return true;
return false;
}
function numberValidate(e) {
var char = String.fromCharCode(e.keyCode);
if (commonPermitted(e)) return;
if (!/^[0-9]/.test(char))
e.preventDefault();
}
function textValidate(e) {
var char = String.fromCharCode(e.keyCode);
if (commonPermitted(e)) return;
if (!/^[0-9a-zA-Z\u0600-\u06FF. -]/.test(char))
e.preventDefault();
}
请注意,如果您的代码已贬值,请改用或kbdEvent.keyIdentifier,但不要假设您支持它们我刚刚使用了您的ctrl部分代码,谢谢您的支持。如果我想检查整个输入粘贴,我能做什么?我的意思是使用粘贴功能。如何编写或更改我的函数以检查我的值不会有我不允许的特殊字符。再次感谢