var myReg = /^[1-9][0-9]{1,2}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/;
alert(myReg.test(val));

解决方案 »

  1.   

    IP地址规则不太了解
    <  ....onblur="checkIP(this.value)"...>
    function checkIP(val){
       if(没有小数点) return false;
       else{
          var ss = val.split(".");
          if(ss.length<4) return false;
          else{
              for(var i=0;i<ss.length;i++)
                  if(ss[i]不合法) return false;
          }
       }
    }
    把编码规则加进去就行了
    }
      

  2.   

    function IsIP(str)
    {
      var a = str.split(".")
      if(a.length!=4) return false;
      for(var i=0; i<4; i++)
        if(a[i]<0 || a[i]>255) return false;
      return true;
    }wangxiaomax(缺省值) 的代码是错的: 比如说: 192.999.999.999
      

  3.   

    时间不多,写的不够完善,自己改改也能用
    <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;
    }
    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>
      

  4.   

    to: zhaoxiaoyang(梅雪香) 
      格式判断的很不错,但是每个Ip地址市0-255的整数,这个没有判断阿!
      不好意思,我以前从没有用过javaSript
     请回复!
      

  5.   

    支持用 meizz(梅花雪) 的
      

  6.   

    梅花雪大哥,小弟在你的基础上改一下,多有犯冒,请谅解。
    function IsIP(str)
    {
      var a = str.split(".")
      if(a.length!=4) return false;
      if(a[0]==127) return fasle;   //127开头不是的。
      for(var i=0; i<4; i++)
        if(a[i]<0 || a[i]>255) return false;
      return true;
    }
    支持梅花雪。
      

  7.   

    <script>
    function aa(){
    var a,b,c,i,d
    c=1
    a=document.from1.text1.value
    b=a.length
    if (b>=7>{
       if(b<=15){
           for(i=0;i<=b;i++){
           c++
               if(a.slubstring(i,c)="."){
                     d=d+1
                     if(d!=4){
                         alert("您输入的不是IP")
                     else{
                    i=-1,c=0
                    if(a.substrig(i,c)%2==0){
                        if(a.substring(i,c)%2==1){
                            alert("你输入的是IP")
                                             }
                        else{
                            alert("您输入的是IP")
                            }
                              }
                             }                             
                            }
                                      }
                         }
                  }
                }    
       }                      
    </script>
    <from name="form1" action="">
    <input type="text" name="text1">
    <input type="submit" name="submit" onclick="aa()">
    </from>