Solved

Validate TEdit to allow only valid file name characters?

Posted on 2015-02-19
6
312 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

707 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