prevent user from entering non ISO-8859-1 characters 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
mael15
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 540
Joined: Fri May 22, 2009 8:52 am
Location: Bremen, Germany

prevent user from entering non ISO-8859-1 characters

Post by mael15 »

Hi,
I would like to prevent the user from entering non ISO-8859-1 characters into a wxTextCtrl. First choice would be if one could use a wxTextValidator, i.e. like wxFILTER_ASCII only for ISO-8859-1? Would I have to create my own include list with all possible characters?
Second option: somehow test the input for illegal characters and warn the user about them.
Is it possible somehow?
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7480
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: prevent user from entering non ISO-8859-1 characters

Post by ONEEYEMAN »

Hi,
What platform?
What wx version?
Did you look at validate sample to see if its easy enough to do?

Thank you.
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: prevent user from entering non ISO-8859-1 characters

Post by doublemax »

Try to encode the char in ISO8859-1. If the result is empty, the char can't be represented in that encoding.

Code: Select all

wxTextCtrl *tc = new wxTextCtrl( panel, wxID_ANY, "", wxPoint(10,10), wxSize(200,-1) );
tc->Bind( wxEVT_CHAR, &MyFrame::OnChar, this );


void MyFrame::OnChar(wxKeyEvent &event)
{
  wxChar c = event.GetUnicodeKey();
  wxString s(c);
  // wxLogDebug("%d %04x %s", c, c, s);
  if(strlen(s.mb_str(wxConvISO8859_1)) == 1)
  {
    // char is ok, let the char be handled
    event.Skip();
  }
  // char is not ok, so we will "consume" it
}
Beware that copy/paste does not trigger the wxEVT_CHAR handler, so it's still possible to get "wrong" chars into the control that way.
Use the source, Luke!
mael15
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 540
Joined: Fri May 22, 2009 8:52 am
Location: Bremen, Germany

Re: prevent user from entering non ISO-8859-1 characters

Post by mael15 »

ONEEYEMAN wrote: Tue Jul 21, 2020 4:13 pm What platform?
What wx version?
Did you look at validate sample to see if its easy enough to do?
wxMSW, wx version 3.1.3.
I looked into the docs and the validation sample and found it quite complicated to list every single valid character from ISO-8859-1 by hand and then use the include list with a wxValidator. IMHO that should be part of a future wxWidgets release?
doublemax wrote: Tue Jul 21, 2020 4:56 pm if(strlen(s.mb_str(wxConvISO8859_1)) == 1)
That is really nice, thanx! I use it in a wxEVT_TEXT to also include copy and pasted text.

Code: Select all

if (strlen(newVal.mb_str(wxConvISO8859_1)) == 0) {
	wxMessageBox(_i("Es wurde ein nicht speicherbares Zeichen eingeben. Nur Zeichen nach\nISO-8859-1 sind erlaubt. Ungültige Zeichen wurden entfernt."), _i("Fehler"));
	wxString correctedStr;
	for (auto chr : newVal)
		if (strlen(wxString(chr).mb_str(wxConvISO8859_1)) == 1)
			correctedStr.append(chr);
	newVal = correctedStr;
}
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4204
Joined: Sun Jan 03, 2010 5:45 pm

Re: prevent user from entering non ISO-8859-1 characters

Post by PB »

mael15 wrote: Sun Jul 26, 2020 10:57 am I looked into the docs and the validation sample and found it quite complicated to list every single valid character from ISO-8859-1 by hand and then use the include list with a wxValidator. IMHO that should be part of a future wxWidgets release?
Let's hope not. This is IMO a very uncommon task. In 2020+, oneeither usually limits the characters to printable 7-bit ASCII characters (subset), allows everything, or writes a custom validator. In this connected world, limiting text to a certain code page is a thing of past...
Post Reply