慕桂英4014372
类似于PB的editmask的可自行设置mask的输入控件input.htm<html><head><title>格式化输入控件 code by meixx</title><style type="text/css">.txtMask{ background-color:#FFFFCC; behavior: url(meixx.htc);}</style><script language="JavaScript"><!--//--></script></head><body ><table cellpadding=0 cellspacing=0 border=1 width="100%"><tr height="200"><td><H4>格式化输入控件</h4><p>模式:##-###-########<p>说明:可自行更改输入的格式,但只能用#作为输入部分(暂时只能输入数字),分隔部分字符不限!<p> 合法检查函数根据需要自己完成<p>引用方法: <input type="text" check="date" mask="####-##-## ##:##" class="txtMask"....><p>css样式: .txtMask{ background-color:#FFFFCC; behavior: url(meixx.htc);}<p>code by meixx<p>2005-04-06<br><br></td></tr><tr><td>日期时间输入:<input type="text" name="txtMask" check="date" id="txtMask" mask="####-##-## ##:##" size=20 class="txtMask">例如可输入:2005-04-06 15:00</td></tr></table></body></html>********************************************************//meixx.htc<!----><!--接口定义--><public:component><public:property name="description" value="Mask Input Behavior" /><public:property name="version" value="1.0.0.0" /> <public:attach event="oncontentready" onevent="init()" /><public:attach event="onselectstart" onevent="eventCancel()" /><public:attach event="onkeypress" onevent="keyPress()" /><public:attach event="onkeydown" onevent="keyDown()" /><public:attach event="onPaste" onevent="eventCancel()" /><public:attach event="onFocus" onevent="doFocus()" /><public:attach event="onclick" onevent="doFocus()" /><public:attach event="ondblclick" onevent="doFocus()" /><public:attach event="onblur" onevent="doCheck()" /> <public:method name="getCursorPos" /><public:method name="setCursorPos" /><public:method name="movCursorNext" /><public:method name="IsNumber" /></public:component><!--组件实现--><script language="javascript">var mask=element.mask;//内部函数,事件oncontentready,初始化function init(){//alert();element.value=mask;element.maxLength=mask.length;}function keyPress(){//如果输入不是数字,返回var keyNum=String.fromCharCode(event.keyCode)if(!IsNumber(keyNum)) return false;writeNum(keyNum,1);var curPos=getCursorPos();if(mask.charAt(curPos)!="#"){MoveCursor(1);}}function keyDown(){var e=window.event;var code=e.keyCode;// alert(code);if(code==8 || code==37 || code==39){var curPos=getCursorPos();if(code==8){ //退格时替换内容if(mask.charAt(curPos-1)!="#")MoveCursor(-1);curPos=getCursorPos();writeNum(mask.charAt(curPos-1),-1);window.event.returnValue=false;MoveCursor(-1);}else{if(code==37 && mask.charAt(curPos-1)!="#")MoveCursor(-1);else if(code==39 && mask.charAt(curPos+1)!="#")MoveCursor(1);}}if(code==46) window.event.returnValue=false;}//替换文本框中光标所在位置的下一个或上一个字符为输入值或默认的初始值//flag 1:输入 -1: 退格function writeNum(keyNum,flag){var rng=document.selection.createRange();if(flag>0)rng.moveEnd("character",flag);elserng.moveStart("character",flag);//alert(rng.text);rng.text=keyNum;}//取得当前光标位置function getCursorPos(){var len=mask.length;var pos=0;var rng=document.selection.createRange();rng.moveEnd("character",len);try{pos=len-rng.text.length}catch(e){}return pos;}//向前或向后移动光标一位 flag 1:向前 -1 :向后function MoveCursor(flag){var rng =element.createTextRange();rng.moveStart("character",getCursorPos()+flag);rng.collapse(true);rng.select();}//判断keypress事件输入是否为数字true:是 false:不是function IsNumber(keyNum){return !isNaN(parseInt(keyNum));}//取消事件function eventCancel(){with (window.event){cancelBubble = true ;returnValue = false ;}return false ;}//获得焦点事件function doFocus(){if(mask.charAt(getCursorPos())!="#")MoveCursor(1);}//统一的检查函数function doCheck(){//根据需要检查录入的内容是否合法switch(element.check){case "date": checkDate(); break;default: return;}}function checkDate(){}</script>放在一个目录下即可运行