{//掩码varexpMask=newRegExp(mask,"g");//当前文本框中的文本varstrText=objTextBox.value;//文本长度varnTextLen=strText.length;//当前光标位置varnCursorPos=getPos(objTextBox);//按下的键码varnKeyCode=window.event.keyCode;varvResult="";if(nKeyCode>95) nKeyCode-=(95-47);//封住传统处理window.event.returnValue=false;if(nKeyCode==8) nKeyCode=37;//按退格键时向左移动光标//自行处理按钮switch(nKeyCode)
{case38://如果动作是方向键[上]case39://如果动作是方向键[右]{
nCursorPos++;break;
}case37://如果动作是方向键[左]case40://如果动作是方向键[下]{
nCursorPos--;break;
}default:
{if(nKeyCode!=13){//不是回车键strText=strText.substr(0,nCursorPos)+String.fromCharCode(nKeyCode)+strText.substr(nCursorPos+1,nTextLen);
};
nCursorPos++;if(nCursorPos>strText.length)
{
nCursorPos=strText.length;
}break;
}
}
vResult=strText.match(expMask);if(vResult!=null)
{//输入格式正确objTextBox.value=strText;
}else{
nCursorPos--;
};//移动光标setCursor(objTextBox,nCursorPos);
}//得到一个文本框控件的当前光标位置functiongetPos(obj)
{
obj.focus();varworkRange=document.selection.createRange();
obj.select();varallRange=document.selection.createRange();
workRange.setEndPoint("StartToStart",allRange);varlen=workRange.text.length;
workRange.collapse(false);
workRange.select();returnlen;
}//设置一个文本框控件的当前光标位置functionsetCursor(obj,num){
range=obj.createTextRange();
range.collapse(true);
range.moveStart('character',num);
range.select();
}