Solved

Validate TEdit to allow only valid file name characters?

Posted on 2015-02-19
6
275 Views
Last Modified: 2015-05-30
Hi! In a Delphi XE7 VCL application, at runtime I need to validate the text input inside a TEdit to allow only valid file name characters. In Delphi XE7 there is the System.IOUtils.TPath.HasValidFileNameChars function. How can this be applied in real-time in a TEdit component?

Unfortunately, the TEdit component has no OnValidating event. I don't want to use the OnChange event, because it looks ugly when the character appears for a short time. The validation must occur BEFORE the character appears.
0
Comment
Question by:PeterDelphin
  • 2
6 Comments
 
LVL 24

Expert Comment

by:jimyX
ID: 40620411
You can make use of one of the KeyDown/KeyUp/KeyPress events which enables you to check the pressed key before it is printed to the component.

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
  if Key in ['a'..'z', 'A'..'Z','\', ..etc]  // List all the allowed Char ranges
  if not TPath.HasValidFileNameChars(Edit1.Text+Key, ...) // Or check the key if is not valid just reset the key
    Key:= #0;
end;
0
 

Author Comment

by:PeterDelphin
ID: 40623965
This is not reliable, as one can paste still PASTE chars which are not allowed as filename.
0
 
LVL 24

Accepted Solution

by:
jimyX earned 250 total points
ID: 40624087
In this case you have to use OnChange event, but with a unique technique, I will show you how to avoid the invalid-chars appearance.

procedure TForm1.Edit1Change(Sender: TObject);
begin
  if not HasValidFileNameChars(TEdit(Sender).Text,..) then
    begin
      TEdit(Sender).Undo; //Clear last entry it was not valid char
      Exit;
    end;

  TEdit(Sender).ClearUndo; //reset the Undo, when you have a valid entry
end;

Open in new window


Mahdi78 has wrote a wonderful article about utilizing Undo & ClearUndo. It worth reading it.
0
 
LVL 37

Assisted Solution

by:Geert Gruwez
Geert Gruwez earned 250 total points
ID: 40660415
use a maskedit to allow only the characters you want
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question