Home > Lotus Notes Solution, Lotus Notes Tutorial, Notes Designer > Lotus Notes IP Address Validation

Lotus Notes 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.

Viewed 7992 times by 2658 viewers

  1. w3cvalidation
    April 12th, 2010 at 16:18 | #1

    Nice information, I really appreciate the way you presented.Thanks for sharing..

  1. No trackbacks yet.