John_Saunders
09-12-2002, 03:27 PM
I found the following IP Address validator in Javascript but I would like it to be in PHP. Does anybody know how to convert it into PHP? Or know of a script like this already written in PHP?
What I would like it to do is just display an error if there is a problem on the next page, instead of saying 'valid ip address'.
<html>
<head>
<!--
This file retrieved from the JS-Examples archives
http://www.js-examples.com
100s of free ready to use scripts, tutorials, forums.
Author: JS-Examples - http://www.js-examples.com/
-->
<script>
function verifyIP (IPvalue) {
errorString = "";
theName = "IPaddress";
var ipArray=IPvalue.split(".");
if (IPvalue == "0.0.0.0")
errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.(4)';
else if (IPvalue == "255.255.255.255")
errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.(5)';
if (!ipArray||!ipArray[0]||!ipArray[1]||!ipArray[2]||!ipArray[3]||ipArray.length>4)
errorString = errorString + theName + ': '+IPvalue+' is not a valid IP address.(1)';
else {
for (i = 0; i < 4; i++) {
thisSegment = ipArray[i];
if (thisSegment > 255) {
errorString = errorString + theName + ': '+IPvalue+' is not a valid IP address.(2)';
i = 4;
}
if ((i == 0) && (thisSegment > 255)) {
errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.(3)';
i = 4;
}
}
}
extensionLength = 3;
if (errorString == "")
alert ("That is a valid IP address.(0)");
else {
alert (errorString);
return false;
}
return false;
}
</script>
</head>
<body>
This will validate the input box's value to be an IP number.
<form name=f1>
IP Address:<INPUT type="text" id=txtIPAddress value="255.255.255.255">
<BR>
<input type=text name=result value="?">
<INPUT type="button" value="IPvalue"
onclick= "document.f1.result.value=verifyIP(document.f1.txtIPAddress.value)">
</form>
</body>
</html>
John
What I would like it to do is just display an error if there is a problem on the next page, instead of saying 'valid ip address'.
<html>
<head>
<!--
This file retrieved from the JS-Examples archives
http://www.js-examples.com
100s of free ready to use scripts, tutorials, forums.
Author: JS-Examples - http://www.js-examples.com/
-->
<script>
function verifyIP (IPvalue) {
errorString = "";
theName = "IPaddress";
var ipArray=IPvalue.split(".");
if (IPvalue == "0.0.0.0")
errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.(4)';
else if (IPvalue == "255.255.255.255")
errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.(5)';
if (!ipArray||!ipArray[0]||!ipArray[1]||!ipArray[2]||!ipArray[3]||ipArray.length>4)
errorString = errorString + theName + ': '+IPvalue+' is not a valid IP address.(1)';
else {
for (i = 0; i < 4; i++) {
thisSegment = ipArray[i];
if (thisSegment > 255) {
errorString = errorString + theName + ': '+IPvalue+' is not a valid IP address.(2)';
i = 4;
}
if ((i == 0) && (thisSegment > 255)) {
errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.(3)';
i = 4;
}
}
}
extensionLength = 3;
if (errorString == "")
alert ("That is a valid IP address.(0)");
else {
alert (errorString);
return false;
}
return false;
}
</script>
</head>
<body>
This will validate the input box's value to be an IP number.
<form name=f1>
IP Address:<INPUT type="text" id=txtIPAddress value="255.255.255.255">
<BR>
<input type=text name=result value="?">
<INPUT type="button" value="IPvalue"
onclick= "document.f1.result.value=verifyIP(document.f1.txtIPAddress.value)">
</form>
</body>
</html>
John