API
@-Formulas
JavaScript
LotusScript
Reg Exp
Web Design
Notes Client
XPages
 
IP Address Validation
We recently had a need on an intranet site to validate the format of IP addresses being entered. Remember that IP addresses must have 4 parts, separated by periods, and that each part must be in the range of 0 to 255. Also note that "000" for a part would be valid, as would "0".

So, the first thing we do is make sure there are 4 groups of numbers (anywhere from 1 to 3 digits) separated by periods. Use a regular expression to do that. If the string passes that test, then split up the string into its 4 parts. The first part cannot be the number zero, so a check is done for that. None of the parts can be greater than 255, so just use a little loop to check the value of each part to make sure it is less than or equal to 255. If everything passes, return true, otherwise return false.

function isValidIPAddress(ipaddr) {
   var re = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
   if (re.test(ipaddr)) {
      var parts = ipaddr.split(".");
      if (parseInt(parseFloat(parts[0])) == 0) { return false; }
      for (var i=0; i<parts.length; i++) {
         if (parseInt(parseFloat(parts[i])) > 255) { return false; }
      }
      return true;
   } else {
      return false;
   }
}


Note that we use "parseInt(parseFloat(...))" for checking. If we just used "parseInt" then if the user entered "08" for the first part, the "parseInt" would return 0, which would fail even though the number is valid.