当前位置: 首页 > 图文教程 > 网络编程 > Javascript > 关于IP验证的一个例子

Javascript
form中限制文本字节数js代码
use jscript with List Proxy Server Information
use jscript List Installed Software
List Installed Software Features
List Information About the Binary Files Used by an Application
List the Codec Files on a Computer
List the UTC Time on a Computer
List Installed Hot Fixes
excel操作之Add Data to a Spreadsheet Cell
Add Formatted Data to a Spreadsheet
Apply an AutoFormat to an Excel Spreadsheet
JavaScript语法着色引擎(demo及打包文件下载)
类之Prototype.js学习
一款JavaScript压缩工具:X2JSCompactor
iis6+javascript Add an Extension File
jscript之Open an Excel Spreadsheet
jscript之Read an Excel Spreadsheet
jscript之List Excel Color Values
去除图像或链接黑眼圈的两种方法总结
Add a Formatted Table to a Word Document

Javascript 中的 关于IP验证的一个例子


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-10   浏览: 300 ::
收藏到网摘: n/a

平时感觉Windows的IP输入框很好用,如果再网页中也能实现一定是很爽的事情。核心代码如下:

<script language="javascript">
function outStr(){
 if(document.all.ip1.value>0 && document.all.ip2.value>0 && document.all.ip3.value>0 && document.all.ip4.value>0){
  alert("你输入的IP地址为:" + document.all.ip1.value + "." + document.all.ip2.value + "." + document.all.ip3.value + "." + document.all.ip4.value);
 }//end if
}//end
function moveRight(obj){
 id = parseInt(obj.name.substr(2,1))
 if(id<4){
  eval("document.all.ip" + (id +1) + ".focus()");
 }else{
  outStr();
 }//end if
}
function moveLeft(obj){
 id = parseInt(obj.name.substr(2,1))
 if(id>1){
  eval("document.all.ip" + (id -1) + ".focus()");
 }//end if
}//end if
function Keypress(obj){
 var objInput = obj;
 cod = window.event.keyCode;
 
 if(cod==46 && (obj.value).length>0){//如果按了“.”并且有了一个字符,向右移动一次
  window.event.keyCode=0;
  moveRight(obj);
 }else if((obj.value).length==2 && (cod >= 48) && (cod <= 57)){//如果数字键并且够了两位
  if(cod<58){//
   obj.value = obj.value * 10 + cod - 48;
  }//end if
  window.event.keyCode = 0;
  moveRight(obj);
 }else{ //判断输入的是不是数字
  if ( !(((cod >= 48) && (cod <= 57)) || (cod == 13) || (cod == 37) || (cod == 39))){
   window.event.keyCode = 0;
  }//end if
 }//end if
}//end function keydown
function getCaretPos(obj){
 var currentRange=document.selection.createRange();
 var workRange=currentRange.duplicate();
 obj.select();
 var allRange=document.selection.createRange();
 var len=0;
 while(workRange.compareEndPoints("StartToStart",allRange)>0){
  workRange.moveStart("character",-1);
  len++;
 }
 currentRange.select();
 return len;
}//end
function KeyDown(obj){
 cod = window.event.keyCode;
 i = getCaretPos(obj); //光标位置
 n = obj.value.length; //字符长度
 
 if(cod==37 && i==0){//光标左移
  moveLeft(obj);
 }else if(cod==39 && i>=n){
  moveRight(obj);
 }else if(cod==8 && (obj.value=="" || i==0)){
  moveLeft(obj);
 }//end if
}//end
function checkDate(obj,max_num){
 if(obj.value>max_num || obj.value<0){
  window.event.keyCode = 0;
  alert(obj.value + "是无效的项目,只能输入0到" + max_num + "之间的数。");
  obj.value = max_num;
  obj.focus();
  obj.select();
 }//end if
}//end
</script>

测试网址:http://www.yemaweb.com/test/ipv1.0.htm
源代码全部在网页中,没有任何加密,希望使用时不要去掉出处。