<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<style>
.a3{width:30;border:0;text-align:center}
</style>
<script>
function mask(obj){
obj.value=obj.value.replace('.','');
key1=event.keyCode
if(!validate(obj.value)){
obj.value="";
}
if(key1==190||key1==110){
nextT(obj);
}
}function nextT(obj){
obj.blur();
nextip=parseInt(obj.name.substr(2,1));
nextip=nextip+1;
nextip=nextip>=5?1:nextip;
nextip=nextip<=0?4:nextip;
var nextT = document.getElementById("ip"+nextip)
nextT.select();

}
function validate(ip){
return parseInt(ip)<=256&&parseInt(ip)>=0;
}
function  mask_c(obj)
{
clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))
}
</script>
<title></title></head>
</HEAD><BODY>
<div  style="border-width:2;border-color:balck;border-style:solid;width:165;font-size:9pt">
<input  type=text  name=ip1 maxlength= 3 class=a3  onkeyup="mask(this)"  onbeforepaste=mask_c()>.
<input  type=text  name=ip2 maxlength= 3 class=a3  onkeyup="mask(this)"  onbeforepaste=mask_c()>.
<input  type=text  name=ip3 maxlength= 3 class=a3  onkeyup="mask(this)"  onbeforepaste=mask_c()>.
<input  type=text  name=ip4 maxlength= 3 class=a3  onkeyup="mask(this)"  onbeforepaste=mask_c()>
</div>
</BODY>
</HTML>
网上找到一个例子。修改了点。不过还是有些问题。

解决方案 »

  1.   

    http://community.csdn.net/Expert/topic/3914/3914084.xml?temp=.368664
    类似于PB的editmask的可自行设置mask的输入控件<!--下面是很久以前写的,不是很好用,需要你自己加上输入校验-->
    <html>
    <head>
    <title>无标题文档</title>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <script language="javascript">
    function keydown(src){
    var e=window.event;
    var code=e.keyCode;
    var cursorPos=getPos(src);
    if(code==8){ //退格
    if(cursorPos==4 || cursorPos==8|| cursorPos==12){
    movenext(src,cursorPos,-1);
    cursorPos-=1;
    }else movenext(src,cursorPos,0);
    var range=setSelect(src,cursorPos-1);
    range.text=" ";
    movenext(src,cursorPos,-1);
    e.returnValue=false;
    return false;
    }else if(code==46)  e.returnValue=false;
    }
    function keypress(src){
    var e=window.event;
    var code=e.keyCode;
    var cursorPos=getPos(src);
    if(code==46){
    if(cursorPos<13) 
    movenext(src,cursorPos,cursorPos<4?(4-cursorPos):(cursorPos<8?(8-cursorPos):(cursorPos<12?(12-cursorPos):(0))));
    }
    if(!keyPressInt()) return false;
    if(cursorPos==3 || cursorPos==7|| cursorPos==11){
    movenext(src,cursorPos,1);
    cursorPos += 1;
    }
    var num = String.fromCharCode(code);
    if(checkInput(src,num,cursorPos)){
    var range=setSelect(src,cursorPos);
    range.text=num;
    if(cursorPos==2 || cursorPos==6|| cursorPos==10)
    movenext(src,cursorPos,2);
    else movenext(src,cursorPos,1);
    }
    e.returnValue=false;
    return false;
    }
    /*
    src 文本控件的引用
    num 输入的字符,这里只能是数字
    pos 光标位置,根据位置不同校验是否允许输入
    */
    function checkInput(src,num,pos){
    /* 在这里进行每个位置的输入合法判断*/
    return true;
    }
    function setSelect(src,pos){
    var range = src.createTextRange();
    range.moveStart('character',pos); 
    range.moveEnd('character',pos-src.value.length+1); 
    range.select();
    return range;
    }
    function getPos(obj){
    var ml=obj.value.length;
    var pos=0;
    var rng=document.selection.createRange();
    rng.moveEnd("character",ml);
    try{
    pos=ml-rng.text.length
    }catch(e){}
    return pos;
    }
    function movenext(src,pos,flag)
    {
    var range =src.createTextRange();
    range.moveStart("character",pos+flag);
    range.collapse(true);
    range.select();
    }
    function keyPressInt(){
    var e=window.event;
    code=e.keyCode;
    if(code >=48 && code <=57)  return true;
    else  {window.event.returnValue=false;return false;}
    }
    function checkPaste(){
    window.event.returnValue=false;
    }
    </script>
    </head><body>
    <p align=center>
    <input name="dateinput" value="   .   .   .   " onkeydown="keydown(this)" size="15" maxlength="15" type="text" onFocus="" onkeypress="keypress(this)" onpaste="checkPaste()" onDrag="checkPaste()" oncut="checkPaste()" onmousemove="checkPaste()">
    </p>
    </body>
    </html>