论坛交流
首页办公自动化| 网页制作| 平面设计| 动画制作| 数据库开发| 程序设计| 全部视频教程
应用视频: Windows | Word2007 | Excel2007 | PowerPoint2007 | Dreamweaver 8 | Fireworks 8 | Flash 8 | Photoshop cs | CorelDraw 12
编程视频: C语言视频教程 | HTML | Div+Css布局 | Javascript | Access数据库 | Asp | Sql Server数据库Asp.net  | Flash AS
当前位置 > 文字教程 > asp教程
Tag:入门,文摘,实例,技巧,iis,表单,对象,上传,数据库,记录集,session,cookies,存储过程,注入,分页,安全,优化,xmlhttp,fso,jmail,application,防盗链,stream,组件,md5,乱码,缓存,加密,验证码,算法,ubb,正则表达式,水印,,日志,压缩,url重写,控件,函数,破解,触发器,socket,ADO,初学,聊天室,留言本,视频教程

javascript做的数据校验(校验IP地址等)

文章类别:asp | 发表日期:2008-10-5 20:53:25

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>New Page 1</title>
</head>

<body>

<script language = 'javascript'>
//校验是否为英文
function checkchar()
{
  var Letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  for (i=0; i < document.form.aaa.value.length; i++)
  {
   var CheckChar = document.form.aaa.value.charAt(i);
   CheckChar = CheckChar.toUpperCase();
   if (Letters.indexOf(CheckChar) == -1)
   {
    alert ("输入类型必须为英文!");
    return false;
   }
  }
}
  
//校验是否为数字

function checknum(p)
{
if (p == "")
{alert ("输入不能为空!");
  return false;
}
var l = p.length;
var count=0;
for(var i=0; i<l; i++)
{
var digit = p.charAt(i);
if(digit == "." )
{
  ++count;
  if(count>1)
  {alert ("输入类型必须为数字!");
   return false;
  }
  }
else if(digit < "0" || digit > "9")
  {alert ("输入类型必须为数字!");
   return false;
  }
  }
return true;
}
  
//校验IP地址格式
function checkip()
{
var scount=0;
var ip = document.form3.ccc.value;
var iplength = ip.length;
var Letters = "1234567890.";
for (i=0; i < document.form3.ccc.value.length; i++)
  {
   var CheckChar = document.form3.ccc.value.charAt(i);
   if (Letters.indexOf(CheckChar) == -1)
   {
    alert ("IP地址格式不对!!,只能输入数字和“.”,格式为XXX.XXX.XXX.XXX 例如:192.168.0.1");
    document.form3.ccc.value="";
    document.form3.ccc.focus();
    return false;
   }
  }

for (var i = 0;i<iplength;i++)
  (ip.substr(i,1)==".")?scount++:scount;
if(scount!=3)
{
  alert ("IP地址格式不对!,只能输入数字和“.”,格式为XXX.XXX.XXX.XXX 例如:192.168.0.1");
    document.form3.ccc.value="";
    document.form3.ccc.focus();
  return false;
}

first = ip.indexOf(".");
last = ip.lastIndexOf(".");
str1 = ip.substring(0,first);
subip = ip.substring(0,last);
sublength = subip.length;
second = subip.lastIndexOf(".");
str2 = subip.substring(first+1,second);
str3 = subip.substring(second+1,sublength);
str4 = ip.substring(last+1,iplength);

if (str1=="" || str2=="" ||str3== "" ||str4 == "")
{alert("数字不能为空!格式为XXX.XXX.XXX.XXX 例如:192.168.0.1");
    document.form3.ccc.value="";
    document.form3.ccc.focus();
return false;
}
if (str1< 0 || str1 >255)
{alert ("数字范围为0~255!");
    document.form3.ccc.value="";
    document.form3.ccc.focus();
    return false;
}
else if (str2< 0 || str2 >255)
{alert ("数字范围为0~255!");
    document.form3.ccc.value="";
    document.form3.ccc.focus();
return false;
}
else if (str3< 0 || str3 >255)
{alert ("数字范围为0~255!");
    document.form3.ccc.value="";
    document.form3.ccc.focus();
return false;
}
else if (str4< 0 || str4 >255)
{alert ("数字范围为0~255!");
    document.form3.ccc.value="";
    document.form3.ccc.focus();
return false;
}

//校验电话号码
function checkphone()
{
var Letters = "1234567890,";
for (i=0; i < document.form4.ddd.value.length; i++)
  {
   var CheckChar = document.form4.ddd.value.charAt(i);
   if (Letters.indexOf(CheckChar) == -1)
   {
    alert ("电话号码只能为数字或逗号!");
    return false;
   }
  }
}


}



</script>

<form name = "form" method="POST" action = "aa" onsubmit ="return checkchar()">
<p>校验英语字母:<input type="text" name="aaa" size="20">
</p>
<p>&nbsp;
<input type="submit" value="提交" name="B1">
<input type="reset" value="全部重写" name="B2"></p>
</form>

<form name = "form2" method="POST" onsubmit= "return checknum(document.form2.bbb.value)">
<p>校验数字:<input type="text" name="bbb" size="20"></p>
<p><input type="submit" value="提交" name="B1"><input type="reset" value="全部重写" name="B2"></p>
</form>

<form name = "form3" method="POST" onsubmit = "return checkip()">
<p>校验IP地址:<input type="text" name="ccc" size="20"></p>
<p><input type="submit" value="提交" name="B1"><input type="reset" value="全部重写" name="B2"></p>
</form>

<form name = "form4" method="POST" onsubmit = "return checkphone()">
<p>校验电话号码:<input type="text" name="ddd" size="20"><input type="submit" value="提交" name="B1"><input type="reset" value="全部重写" name="B2"></p>
</form>

</body>

</html>
视频教程列表
文章教程搜索
 
Asp推荐教程
Asp热门教程
看全部视频教程
购买方式/价格
购买视频教程: 咨询客服
tel:15972130058