Is there an IP address input control? Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Byron0914
Knows some wx things
Knows some wx things
Posts: 38
Joined: Wed Jul 28, 2010 11:55 am

Is there an IP address input control?

Post by Byron0914 »

I have an situation that let the user input IP address through GUI, I can't find IP address input control in wxwidgets, should I use a text control instead for inputing, how can I validate that the user input valid IP address?

Or is there some exist code for IP address input control, please help me, thanks.[/b]
Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria »

If you're using wxPython, you can use the masked text control I believe for this; a C++ version of this control was partially developped this summer during google summer of code, but I gather it's not ready

Otherwise you can probably just use a text control with a custom validator
"Keyboard not detected. Press F1 to continue"
-- Windows
TrV
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 630
Joined: Wed Jul 04, 2007 1:12 pm

Re: Is there an IP address input control?

Post by TrV »

Byron0914 wrote:[...]how can I validate that the user input valid IP address?
For this kind of things, regex does it!

Code: Select all

// check of IP address
// regular expression for IP address (4 [0-255] decimal numbers separated by periods)
// [0-9]{1} : exactly 1 decimal digit ([0-9])
// |[0-9]{2} : ...or exactly 2 decimal digits ([10-99])
// |[0-1][0-9]{2} : ...or '0'/'1' followed by exactly 2 decimal digits ([000-199])
// |2[0-4][0-9] : ...or 2 followed by '0'/'1'/'2'/'3'/'4' followed by 1 decimal digit ([200-249])
// |25[0-5] : ...or '25' followed by '0'/'1'/'2'/'3'/'4'/'5' ([250-255])
// (([0-9]{1}|[0-9]{2}|[0-1][0-9]{2}|2[0-4][0-9]|25[0-5])\\.) : ...the whole followed by '.' (quoted)
// (([0-9]{1}|[0-9]{2}|[0-1][0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3} : ...the whole exactly 3 times
// ^(([0-9]{1}|[0-9]{2}|[0-1][0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3} : ...positioned at start
// ([0-9]{1}|[0-9]{2}|[0-1][0-9]{2}|2[0-4][0-9]|25[0-5]) : [0-9] + [10-99] + [000-199] + [200-249] + [250-255]
// ([0-9]{1}|[0-9]{2}|[0-1][0-9]{2}|2[0-4][0-9]|25[0-5])$ : ...positioned at the end
wxRegEx regxIPAddr("^(([0-9]{1}|[0-9]{2}|[0-1][0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]{1}|[0-9]{2}|[0-1][0-9]{2}|2[0-4][0-9]|25[0-5])$");
if ( !regxIPAddr.Matches(ipAddress) ) {
    ...
}
Moreover, you can limit user input in a textcontrol using a validator:

Code: Select all

//* definition of IP Addresses text validator
wxString ipAddressFilter[11] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "."}; // authorized characters for IP Address

wxArrayString arraystrIPAddress(11, macAddressFilter);

wxTextValidator txtvldIPAddress(wxFILTER_INCLUDE_CHAR_LIST); // text validator for IP Address

txtvldIPAddress.SetIncludes(arraystrIPAddress); // sets authorized characters for IP Address


//* drawing of GUI
this->txtctrlIPAddress = new wxTextCtrl(this, -1, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0, txtvldIPAddress); // displays IP Address edit textcontrol
this->txtctrIPAddress->SetToolTip("Four [0-255] decimal numbers separated by periods\nFormat: A.B.C.D\nEx.: 192.168.1.1");
Byron0914
Knows some wx things
Knows some wx things
Posts: 38
Joined: Wed Jul 28, 2010 11:55 am

Post by Byron0914 »

ok, I get it, Thanks, I think regEx is effective.
Post Reply