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?
prevent user from entering non ISO-8859-1 characters Topic is solved
Re: prevent user from entering non ISO-8859-1 characters
Hi,
What platform?
What wx version?
Did you look at validate sample to see if its easy enough to do?
Thank you.
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
Try to encode the char in ISO8859-1. If the result is empty, the char can't be represented in that encoding.
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.
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
}
Use the source, Luke!
-
- 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
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?
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
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...