Click to See Complete Forum and Search --> : Error in check ip address script


james_cwy
11-19-2003, 12:49 AM
I got this code from the web at
http://javascript.internet.com/forms/val-ip.html but I think there is an error.
When I type for instance 122.2.2.300, it says that it is valid.

<!-- TWO STEPS TO INSTALL VALIDATION (IP ADDRESS):

1. Copy the coding into the HEAD of your HTML document
2. Add the last code into the BODY of your HTML document -->

<!-- STEP ONE: Paste this code into the HEAD of your HTML document -->

<HEAD>

<SCRIPT LANGUAGE="JavaScript">
<!-- Original: Jay Bienvenu -->
<!-- Web Site: http://www.bienvenu.net -->

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- Begin
function verifyIP (IPvalue) {
errorString = "";
theName = "IPaddress";

var ipPattern = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
var ipArray = IPvalue.match(ipPattern);

if (IPvalue == "0.0.0.0")
errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.';
else if (IPvalue == "255.255.255.255")
errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.';
if (ipArray == null)
errorString = errorString + theName + ': '+IPvalue+' is not a valid IP address.';
else {
for (i = 0; i < 4; i++) {
thisSegment = ipArray[i];
if (thisSegment > 255) {
errorString = errorString + theName + ': '+IPvalue+' is not a valid IP address.';
i = 4;
}
if ((i == 0) && (thisSegment > 255)) {
errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.';
i = 4;
}
}
}
extensionLength = 3;
if (errorString == "")
alert ("That is a valid IP address.");
else
alert (errorString);
}
// End -->
</script>

</HEAD>

<!-- STEP TWO: Copy this code into the BODY of your HTML document -->

<BODY>

<form>
IP Address:
<input size=15 name="IPvalue">
<input type="submit" value="Verify" onClick="verifyIP(IPvalue.value)";>
</form>

<p><center>
<font face="arial, helvetica" size"-2">Free JavaScripts provided<br>
by <a href="http://javascriptsource.com">The JavaScript Source</a></font>
</center><p>

<!-- Script Size: 1.91 KB -->

How should I change it so that if the last octet is > 255 then it is invalid. It seems that this script cannot check the last octet.I am not very good with Javascripts. Hope someone can help.

THanks

Charles
11-19-2003, 05:27 AM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<title>Example</title>

<style type="text/css">
<!--
label {display:block; margin:1em 0em}
input {display:block}
-->
</style>

<script type="text/javascript">
<!--
String.prototype.isIPAddress = function () {
if (! /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/.test(this)) return false;
if (RegExp.$1 > 254 || RegExp.$2 > 254 || RegExp.$3 > 254 || RegExp.$4 > 254)
return false;
if (RegExp.$1 < 1 || RegExp.$2 < 1 || RegExp.$3 < 1 || RegExp.$4 < 1) return false;
return true;
}
// -->
</script>

<form action="">
<div>
<label>IP Address<input type="text" onchange="if(!this.value.isIPAddress()) {alert('That would not appear to be a valid IP address.'); this.value = ''; this.focus()}"></label>
<button type="submit">Submit</button>
</div>
</form>

james_cwy
11-19-2003, 07:59 PM
Charles, i tried using your code.
By the way, I am running this code on Mozilla on redhat 9 and cannot seem to get the javascript to work.
Do I need to change anything or can I just use the code that you gave?

Thanks again
James

Charles
11-19-2003, 08:11 PM
What I posted I first tested. If you are having trouble then click on the little "quote" buttonm on the form and cut and paste the quoted version. If you still cannot get it to work then post a link to your page.

james_cwy
11-19-2003, 09:16 PM
I would like to put this on the same line as the submit button and would like the button to be an input type submit.
What I would like here is when the user clicks submit, it will check the validity of the IP address.
If is is valid, then only it will submit successfully to whatever action
else
Javascript will prompt the guy to say wrong IP pls enter again.

How do I change it?
Thanks
James