Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

IP Address input mask

Status
Not open for further replies.

shumi99

Programmer
Mar 3, 2010
30
US
Hi,

Simple question. My brain is not working properly today so here is what I have and what I need to do. I have a "textbox" in a form where the user has to enter an IP Address. As everybody know, the IP address can be:
999.999.999.999
99.999.99.999
9.99.99.9

basically you're getting the idea. There has to be 3 "dots" and a variation of 1 to 3 numbers between. How do I put that in the "Format" or "Input Mask" property?

Any help please.


Thanks,
S #1
 
You can't do this with a Format or InputMask. That's because the position of the dot varies in the three different formats.

The nearest you can get to it is with this InputMask:

Code:
################

(That's 15 hash signs). That will allow the entry of digits and dots, but also plus and minus signs and spaces, which is not what you want. Also, if the programmer has used SET POINT to change the decimal point character, you won't get the dots. Also it doesn't force the dots to be in any particular position.

It might be better to validate the IP address after it has been entered. Off the top of my head:

Code:
lcTest = <whatever the user entered>
lcTest = CHRTRAN(lcTest, "0123456789", REPLICATE("9", 15))
IF INLIST(lcTest , ;
  "999.999.999.999", "99.999.99.999", "9.99.99.9")
  
  * Format is valid

ELSE
  
  * Invalid format

ENDIF

This might need a bit of refinement, but is should give you the general idea.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro tips, advice, training, consultancy
Custom software for your business
 
Wouldn't it be better that you supply 4 textbox with each allowing 0-255.

Or I think it is better to provide a free entry and validate later using IP APIs. You cannot simply validate an IP address by checking if it is made up of 4 numbers. For example this is not a valid address:

256.1.1.1

Remember IPv6 is getting widely used too, so your entry should support both IPv4 and IPv6.

Cetin Basoz
MS Foxpro MVP, MCP
 
Not really solutions. :( I thought it be easier than that. Thanks guys.
 
I think I would use four text boxes, all with 999 formats and each using the lostfocus method to pad them to the right.

I would then validate the four of them at 'save time' (whatever triggers that in your app - a button or another 'lostfocus') and put them back together as a string.



Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.
 
Mike and Cetin have both given excelent solutions. depending on the situation I have used both of them with great success. So the real responce should be that they are not solutions you want to be bothered with. They however are great suggestions to this problem.

Personaly i have started to exclusively use the validate method over the 4 text boxes because, believe it or not users complained about having to hit the tab then when i made it self advance they complained because some would hit tab and jump 2 boxes so you can never win.

Steve Bowman
Independent Technology, Inc.
CA, USA
 
For an app I'm working on, I've built an IP address control. It contains 4 textboxes and three labels and has code to move automatically from one textbox to the next when the user presses the period, and so forth.

It's not that hard to build a custom control, and then you have what you really want.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top