Solved

Validate TEdit to allow only valid file name characters?

Posted on 2015-02-19
6
287 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Newbie Thread Programming 1 161
Best Firemonkey component pack 1 119
Virtuailstring tree compare node issue 14 125
How to create virtual printer while installing my application 5 61
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

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