JAVASCRIPT做的数据校验(校验IP地址等)
类别: JavaScript教程
<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>
<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>
<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>
<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>
- 上一篇: 用JS制作完善的日,月组合下拉框.
- 下一篇: 使用JAVASCRIPT实现邮箱快速登录的方法
-= 资 源 教 程 =-
文 章 搜 索