Programming Tips - C/C++: simple C/C++ code to validate an internet host name / IP-address

Date: 2015feb6 Language: C/C++ Q. C/C++: simple C/C++ code to validate an internet host name / IP-address A.
// helper inline bool isValidHostChar(const char c) { return isalnum(c) || c == '.' || c == '-' || c == '_'; } // Not perfect but catches most user mistakes bool isValidHost(const char *host) { if (host == NULL) return false; if (strlen(host) < 3) return false; for (const char *p = host; *p; p++) { if (!isValidHostChar(*p)) return false; } return true; }