<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>mac</title>
<script type="text/javascript" src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
$("#mac").keyup(function(){
var CurrentMacContent = $("#mac").val();
$("#mac").val( updateInputMacChar( window.event.keyCode,CurrentMacContent ));
$("#mac").val( forceToNum(CurrentMacContent) ); //用户连续按某个键后的处理
});
});
function updateInputMacChar( MacChar , MacContent ){ //更改用户输入的最后一个字符
var CheckFlag = false;
var NewMacContent = '';
if( typeof( MacChar ) == 'undefined' ) renturn;
if( typeof( MacContent ) == 'undefined' ) renturn;
if( MacChar === '') renturn;
if( MacContent === '') renturn;
CheckFlag = checkInputMacChar(MacChar);
if( CheckFlag == false ){
if( MacContent.split("").length == 1) NewMacContent = ''; //只输入一个字符的情况
for( var p = 0 ; p < MacContent.split("").length-1 ; p++ ){
NewMacContent += MacContent.split("")[p];
}
}else{
NewMacContent = MacContent;
}
return NewMacContent;
}
function checkInputMacChar(MacChar){ //检测是否是合法字符,'1' 'a' 'A' 49 65
var CheckFlag = false;
for( var i = 48; i <= 57 ; i++ ){ //0-9
if( (MacChar == i)){<span style="white-space:pre"> </span>//键盘输入的字符keyCode,即对应asii值
CheckFlag = true;
return CheckFlag;
}
try{<span style="white-space:pre"> </span>//split拆分字符串得到的字符,即对应单个字符,得转换成asii值才能比较。
if( (MacChar.charCodeAt(0) == i)){
CheckFlag = true;
return CheckFlag;
}
}catch(exception){
}
}
if( CheckFlag == false ){
for( var j = 65; j <= 70 ; j++ ){ //a-f
if( MacChar == j ){<span style="white-space:pre"> </span><span style="font-family: Arial, Helvetica, sans-serif;">//键盘输入的字符keyCode,即对应asii值</span><span style="white-space:pre">
</span> CheckFlag = true;
return CheckFlag;
}
try{
if( (MacChar.charCodeAt(0) == j)){<span style="white-space:pre"> </span><span style="font-family: Arial, Helvetica, sans-serif;">//split拆分字符串得到的大写字符,即对应单个字符,得转换成asii值才能比较。</span>
CheckFlag = true;
return CheckFlag;
}
}catch(exception){
}
}
}
if( CheckFlag == false ){
for( var j = 97; j <= 102 ; j++ ){ //a-f
try{
if( (MacChar.charCodeAt(0) == j)){<span style="white-space:pre"> </span><span style="font-family: Arial, Helvetica, sans-serif;">//split拆分字符串得到的小写字符,即对应单个字符,得转换成asii值才能比较。</span><span style="white-space:pre">
</span> CheckFlag = true;
return CheckFlag;
}
}catch(exception){
}
}
}
}
function forceToNum(MacContent){ //检测所有的字符是否有不合法的
var NewMacContent = '';
if( typeof( MacContent ) == 'undefined' ) renturn;
if( MacContent == '') renturn;
for( var i = 0 ; i < MacContent.split("").length ; i++){
if( checkInputMacChar( MacContent.split("")[i] ) ){<span style="white-space:pre"> </span>//只要是非法字符,就用0代替。
NewMacContent += MacContent.split("")[i];
}else{
NewMacContent += '0';
}
}
return NewMacContent;
}
</script>
</head>
<body>
<input type="input" id="mac">
<div id="keycode"></div>
</body>
</html>