Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate hostname (may be IP) and port number (CString)

I have an dialog for HTTP proxy configuration with hostname and port edit boxes.

My question is how can I validate user input for correct hostname and port?

May be I have to use some regular expression for hostname validation or there is some helpful MFC or WinAPI function? What is the maximum correct number for port?

like image 339
Julian Popov Avatar asked Jun 23 '09 15:06

Julian Popov


2 Answers

  1. Port number is an unsigned short from 1-65535, ports under 1024 are reserved for system services http, ftp, etc.
  2. I don't believe that you can validate host name (or should) especially when you have locallized host names.
like image 70
Shay Erlichmen Avatar answered Sep 20 '22 19:09

Shay Erlichmen


You can use Apache Commons Validator's InetAddressValidator. Check out http://commons.apache.org/proper/commons-validator/apidocs/org/apache/commons/validator/routines/InetAddressValidator.html.

That project has many validator utilities you can reuse. Hope it helps.

like image 21
The_Cute_Hedgehog Avatar answered Sep 20 '22 19:09

The_Cute_Hedgehog