Reputation: 69
How to limit acceptable input Japaneses Character on TEdit in Delphi?
I have one TEdit
in Delphi XE8 that I require to input only Japanese Characters, so how can I do that?
Upvotes: 1
Views: 327
Reputation: 2224
As LU RD mentioned it is difficult to distinguish Chinese/Japanese characters, but if you are Ok to test ranges of characters that may be Japanese as defined here you can use following function. You'll need to change you source file format to be Unicode to enter Unicode characters, you can do it by right-clicking in the editor and selecting File Format option.
function IsStringJapanese(const S: string): boolean;
var
C: Char;
begin
Result := True;
for C in S do
begin
Result :=
// kanji
((C >= '一') and (C <= '龿'))
// hiragana
or ((C >= 'ぁ') and (C <= 'ゟ'))
// katakana
or ((C >= '゠') and (C <= 'ヿ'));
if not Result then
begin
Break;
end;
end;
end;
Second question is how to validate TEdit depends on whether you are using VCL or FMX. In VCL you can prevent typing in certain characters by processing KeyPress event as following
procedure TMainForm.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if not IsStringJapanese(Key) then
begin
Key := #0;
end;
end;
but it does not prevent copy/pasting string to Edit, you'll need to validate text before you use it.
In FMX you can use OnValidating event
procedure TForm1.Edit2Validating(Sender: TObject; var Text: string);
begin
if not IsStringJapanese(Text) then
begin
raise Exception.Create('Text not japanese');
end;
end;
Upvotes: 3