Showing posts with label Regular expression for validating the IP Address using Javascript?. Show all posts
Showing posts with label Regular expression for validating the IP Address using Javascript?. Show all posts

Friday 30 March 2012

Regular expression for validating the IP Address using Javascript?

Regular expression for validating the IP Address using Javascript?
[JavaScript]
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;
}
}