Solved

Validate TEdit to allow only valid file name characters?

Posted on 2015-02-19
6
264 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
Comment Utility
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
Comment Utility
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
Comment Utility
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 36

Assisted Solution

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

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now