aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes IP Address / Hostname validation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "IP Address / Hostname validation" Watch "IP Address / Hostname validation" New topic
Author

IP Address / Hostname validation

Bob Wheeler
Ranch Hand

Joined: Apr 24, 2009
Posts: 317
Hi,

I'm just reviewing my application and I'm stuck at the validation of the IP address / host name stuff. To validate a IP address is not difficult. But I have problems with the host name validation. Sure I can call

try {
InetAddress.getByName(hostname);
} catch (UnknownHostException uhe) {
// do something

But in my LAN (with several other computers) it takes hours to do the validation. And because I check the host name after each character input the assessor will probably kill me. So I wonder, is it OK, if I only allow IP addresses or (probably better), if I shouldn't do any validation on the IP address/host name?

Thanks,
Bob


SCJP 6 - SCJD - SCWCD 5 - SCBCD 5
JavaEnterpriseEditionFaq - TomcatFaq
Fernando Franzini
Ranch Hand

Joined: Jan 09, 2009
Posts: 486
    
    2

Hi Bob
if I shouldn't do any validation on the IP address/host name?

In my opinion...there is no such need ! Just validation mask and format is enought...unless your assignment tell you some "must" about it.
I did not even....


Fernando Franzini - Java Blog
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2409
    
    7

Just use IP address is fine. I didn't even bother with hostname. For validation, you can use some regex to do the IP validation. Then depending on your services you can or may do a test connection to verify it is the correct server. Because if not and those config saved to properties file, user will not able to connect forever! Unless you expect them to know the name of the properties file which typically no.

I recall there is a thread I started about IP address validation

Oh forgot about testing server connection, if you do you better display some message saying "Testing connection..." or else user may feel like your app hangs.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
Bob Wheeler
Ranch Hand

Joined: Apr 24, 2009
Posts: 317
Thanks guys,

also, I only save the configuration, if the connection is established successfully. So no need for testing. If the connection takes a while, I change the cursor to the hour glass. That should be enough.
And my user can find the property file. He is not only a very clever guy, he can find this information in the user guide. Of course he shouldn't edit the file, just deleting it, if the file is not accessible any more. Of course he could call his administrator But that's a different topic.

So, thanks again and cheers
Bob
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5266
    
  13

Hi Bob,

Here is also another thread about this issue.

I don't have a clue why you want to check with every keypress if it is a valid ip or host name Why not execute your check when input field lost focus?

Kind regards,
Roel


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Bob Wheeler
Ranch Hand

Joined: Apr 24, 2009
Posts: 317
Hi Roel,
Roel De Nijs wrote:
Here is also another thread about this issue.

I checked that thread, thanks.

Roel De Nijs wrote:
I don't have a clue why you want to check with every keypress if it is a valid ip or host name Why not execute your check when input field lost focus?

Maybe, I'm just overdoing it, but with keypressed I have my Connect or Start button enabled/disabled to the current value(s) (e.g. to the current ip address). If I use the lostFocus, the button enabling (or disabling) has to wait until the text field(s) looses focus.
I didn't check it, but when does the text field looses the focus; before or after the "disabled" connect button is clicked? I mean, the connect button will be only enabled, if the user clickes somewhere. Does he have to click twice? First to loose focus and to enable connect button, and second to click the now enabled connect button.
I hope this is not one of THOSE questions I shouldn't ask

cheers
Bob
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5266
    
  13

Hi Bob,

My connect-button (to connect to the server) is also disabled when input is not valid. And I did it just with the focusLost-event and I also tried to fool the system by entering something invalid, leaving focus in the text field and then trying to click the connect button in various ways (just hitting enter with connect button being default button, mouse click,...) but always I got an error message (informing me the server address was incorrect) and connect-button being disabled.

Kind regards,
Roel
Fernando Franzini
Ranch Hand

Joined: Jan 09, 2009
Posts: 486
    
    2

Hi Dudes

My connect server button stay enabled all time and I just validate input datas(mandatory and format) when it was clicked.
Very simple......
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2409
    
    7

My save button is also disabled by default. As long as there is text (digits or characters) for the path or ip address, the save button enabled. The port is set to 1099 by default.
Bob Wheeler
Ranch Hand

Joined: Apr 24, 2009
Posts: 317
Thanks guys,

I decided to use IP addresses. I want to finish this assignment. No fine tuning any more.
If I have a valid ip address and port number the connect button is enabled (proofed by key pressed event). No validation is done afterwards.
It's working and the assessor should better like it.

cheers
Bob
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: IP Address / Hostname validation