日期:2014-05-16  浏览次数:20433 次

JS 正则验证 键盘监听事件
//页面键盘事件事件 单号正则验证
document.onkeypress=function(){
	var keycode = String.fromCharCode(event.keyCode);
	var re = new RegExp(/^[A-Za-z0-9]+$/);
	var result = re.test(keycode);
	event.returnValue=false;
	var transitNo = document.getElementById('transitNo');
	if(result){
		transitNo.value = transitNo.value+keycode.toUpperCase();
	}else{
		
	}
	if(event.keyCode==13){transitIn();}
}
//监听粘贴事件
document.body.onpaste=function(){
	var val = window.clipboardData.getData("Text");
	var re = new RegExp(/^[A-Za-z0-9]+$/);
	for(var i=0;i<val.length;i++){
		var tmp = val.substr(i,1);		
		if(!re.test(tmp)){
			val = val.replace(tmp,"").toUpperCase();
			i--;
		}
	}
	event.returnValue=false;
	document.getElementById('transitNo').value=val;
}

?