<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
//检查IP格式是否正确
function checkip(){
var ip=document.form1.text4.value;
var letters='1234567890.'; //定义数值的范围
var count=0;
for(var i=0;i<=ip.length;i++){
var ipletter=ip.charAt(i);
if(letters.indexOf(ipletter)==-1){
alert('请输入0-9之间的数字');
return false;
}
}//
for(var i=0;i<ip.length;i++){ //注: 高手看 就是这个查找小数点的代码出问题了
ip.substr(i,1)=='.'? count++:count;
if(count!=3){
alert('IP的地址必须为3个小数点');
return false;
}
}
first=ip.indexOf('.'); //查找第一个小数点位置
last=ip.lastIndexOf('.'); //查找最后一个小数点位置
str=ip.substring(0,last);
second=str.lastIndexOf('.'); //查找第二个小数点位置
str1=ip.substring(0,first);
str2=ip.substring(first+1,second);
str3=ip.substring(second+1,last);
str4=ip.substring(last+1,ip.length);
if(str1<0||str1>255){
alert('IP地址的数值范围在0~255之内');
return false;
}
else if(str2<0||str2>255){
alert('IP地址的数值范围在0~255之内');
return false;
}
else if(str2<0||str2>255){
alert('IP地址的数值范围在0~255之内');
return false;
}
else if(str2<0||str2>255){
alert('IP地址的数值范围在0~255之内');
return false;
}
}function checktext4(){
document.form1.text4.value="";
}</script><style type="text/css"></style>
</head>
<body>
<form name="form1">
<p>检查IP是否正确:
<input type="text" name="text4" value="检查IP是否正确" onclick="checktext4()" />
<input type="button" name="button3" value="检查IP是否正确" onclick="checkip()"/>
</p>
</form>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
//检查IP格式是否正确
function checkip(){
var ip=document.form1.text4.value;
var letters='1234567890.'; //定义数值的范围
var count=0;
for(var i=0;i<=ip.length;i++){
var ipletter=ip.charAt(i);
if(letters.indexOf(ipletter)==-1){
alert('请输入0-9之间的数字');
return false;
}
}//
for(var i=0;i<ip.length;i++){ //注: 高手看 就是这个查找小数点的代码出问题了
ip.substr(i,1)=='.'? count++:count;
if(count!=3){
alert('IP的地址必须为3个小数点');
return false;
}
}
first=ip.indexOf('.'); //查找第一个小数点位置
last=ip.lastIndexOf('.'); //查找最后一个小数点位置
str=ip.substring(0,last);
second=str.lastIndexOf('.'); //查找第二个小数点位置
str1=ip.substring(0,first);
str2=ip.substring(first+1,second);
str3=ip.substring(second+1,last);
str4=ip.substring(last+1,ip.length);
if(str1<0||str1>255){
alert('IP地址的数值范围在0~255之内');
return false;
}
else if(str2<0||str2>255){
alert('IP地址的数值范围在0~255之内');
return false;
}
else if(str2<0||str2>255){
alert('IP地址的数值范围在0~255之内');
return false;
}
else if(str2<0||str2>255){
alert('IP地址的数值范围在0~255之内');
return false;
}
}function checktext4(){
document.form1.text4.value="";
}</script><style type="text/css"></style>
</head>
<body>
<form name="form1">
<p>检查IP是否正确:
<input type="text" name="text4" value="检查IP是否正确" onclick="checktext4()" />
<input type="button" name="button3" value="检查IP是否正确" onclick="checkip()"/>
</p>
</form>
</body>
</html>
解决方案 »
- Flex中写了一个HelloWord的ActionScript,竟然出错了!!~~~~
- 各位求救?这个是用JS文本框算数?我不会!请大家帮忙,
- 一個關于日歷的問題???請幫忙解決!!
- 求个正则:寻找某个单独字符后跟随的数字
- 为什么我在text中不能调用javascript函数??
- 请问如何限制只输入中文字符?
- 如何检查表单中输入的年、月、日是否正确?
- 回车用javascript如何表示,在线等待,加100分
- 请问javascript能做email的发送程序吗?
- 如何截获事件消息?
- 控件ID在Javascript中的问题
- javascript activeXObject 调用系统声音命令(高手过招)
for (var i = 0; i < ip.length; i++) { //注: 高手看 就是这个查找小数点的代码出问题了
ip.substr(i, 1) == '.' ? count++ : count;
if (i == (ip.length - 1)) {//循环完后再检查
if (count != 3) {
alert('IP的地址必须为3个小数点');
return false;
}
}
}
http://topic.csdn.net/t/20041005/21/3428299.html
http://blog.csdn.net/soulspark/archive/2007/05/14/1608272.aspx