Click to See Complete Forum and Search --> : [RESOLVED] Error in IP Validation script (JavaScript)


Silenced
03-21-2006, 09:20 AM
There's a mistake in the script for IP Validation at this URL: http://javascript.internet.com/forms/val-ip.html


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;
}
}
}


The for loop should go like this:


for(i = 1; i < 5; i++)


The reason is that the first element of ipArray is the complete IP and if the for loop ends before i=4 it doesn't check the last segment of the IP address, accepting addresses like 255.255.255.256 .

David Harrison
03-21-2006, 10:54 AM
Nice catch.

LeeU
03-22-2006, 08:47 AM
Thanks. I appreciate it. I'll change it.