Page 1 of 1

prevent user from entering non ISO-8859-1 characters

Posted: Tue Jul 21, 2020 9:35 am
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?

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

Posted: Tue Jul 21, 2020 4:13 pm
by ONEEYEMAN
Hi,
What platform?
What wx version?
Did you look at validate sample to see if its easy enough to do?

Thank you.

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

Posted: Tue Jul 21, 2020 4:56 pm
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.

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

Posted: Sun Jul 26, 2020 10:57 am
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;
}

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

Posted: Sun Jul 26, 2020 12:09 pm
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...