// Validates standard v4 IP address strings from "0.0.0.0" to "255.255.255.255"
// Returns true if IP Address is valid, otherwise false.
bool validateIPv4(string IPAddress) {
istringstream ss(IPAddress);
string octet;
int octetcount = 0;
while (ss) {
if (!getline(ss, octet, '.')) { break; }
istringstream octetstream(octet);
int ioctet;
octetstream >> ioctet;
if ((!octetstream) || (octetstream.peek() != EOF) || (ioctet < 0) || (ioctet > 255)) {
return false;
}
octetcount++;
}
if (octetcount != 4) { return false; }
return true;
}
Posted: March 20, 2023
Return to the snippets listing