Validate Input IP Number

[Total: 0    Average: 0/5]

This will validate a form input box to have a good IP number in it.

Head Code:

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

Body Code:

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> 

Times Viewed: 1

Leave a Reply

Your email address will not be published. Required fields are marked *