Solved

Error in Synax how can I only allow normal chars within a string?

Posted on 2011-03-02
10
151 Views
Last Modified: 2012-05-11
the system only allows normal txt though doesn't allow /?\* etc, if the string has stuff how can Ionly allow A-Z 0-9?? and it would delete the bad chars while leaving the good ones still in the string.
0
Comment
Question by:eNarc
  • 2
  • 2
  • 2
  • +2
10 Comments
 
LVL 8

Expert Comment

by:BdLm
ID: 35016199

you could check the ascii value and reject values out of range  ...
0
 
LVL 19

Accepted Solution

by:
Thommy earned 168 total points
ID: 35016213
Use this function to strip non alphanumeric chars from a string...
function DeleteNonAlphaNumChars(const TheString: string):string;
var
  SrcP, DestP: PChar; 
begin
  SrcP := PChar(TheString);
  SetLength(Result, Length(TheString));
  DestP := PChar(Result);
  while SrcP <> #0 do begin
    if SrcP[0] in ['a'..'z', 'A'..'Z', '0'..'9'] then begin
      DestP[0] := SrcP[0];
      Inc(DestP);
    end;
    Inc(SrcP);
  end; 
  SetLength(Result, DestP - PChar(Result)); 
end;

Open in new window

0
 
LVL 8

Expert Comment

by:BdLm
ID: 35016221
http://en.wikipedia.org/wiki/ASCII   as a reference ascii table

'A' ..... 'X'   -> ascii rannge 65 ....90    ord('A')  gives you the ascii value 65
0
 
LVL 24

Assisted Solution

by:jimyX
jimyX earned 166 total points
ID: 35016276
You can check if a string is in the valid set of chars:
Function DelSpecialChar(temp:String):String;
var
  i : integer;
begin
  result:='';
  for i:= 1 to length(temp) do
    begin
      if temp[i] in ['A'..'Z','a'..'z','0'..'9'] then
        result:= result + temp[i]
    end;
end;

Open in new window

   
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 19

Expert Comment

by:Thommy
ID: 35016345
For performance reasons (highest speed) if have used PCHAR() in function DeleteNonAlphaNumChars...
0
 
LVL 24

Expert Comment

by:jimyX
ID: 35016357
So you can use it this way, let's say your string Str is including the special characters:

Str := 'e/?Na\*rc';
Str := DelSpecialChar(Str);
Showmessage(Str);

Open in new window

0
 
LVL 32

Expert Comment

by:ewangoya
ID: 35019351
try this code
function IsValidChar(const S: char): Boolean ;
begin
  Result := S in ['A'..'Z', 'a'..'z'];
end;

function NormaLizeStr(const AStr: string): string;
var
  I: Integer;
begin
  Result := AStr;
  for I := Length(Result) downto 1 do
    if not IsValidChar(Result[I]) then
      Delete(Result, I, 1);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowMessage(NormaLizeStr('*(kiuer)lp/\'));
end;

Open in new window

0
 
LVL 32

Assisted Solution

by:ewangoya
ewangoya earned 166 total points
ID: 35173840

This is even faster using CharInSet
function IsValidChar(const S: char): Boolean ;
begin
  Result := CharInSet(S, ['A'..'Z', 'a'..'z', '0'..'9']);
end;

function NormaLizeStr(const AStr: string): string;
var
  I: Integer;
begin
  Result := AStr;
  for I := Length(Result) downto 1 do
    if not IsValidChar(Result[I]) then
      Delete(Result, I, 1);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowMessage(NormaLizeStr('*(kiuer)lp/\'));
end;

Open in new window

0
 
LVL 100

Expert Comment

by:mlmcc
ID: 35399280
This question has been classified as abandoned and is being closed as part of the Cleanup Program. See my comment at the end of the question for more details.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This video discusses moving either the default database or any database to a new volume.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

757 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

25 Experts available now in Live!

Get 1:1 Help Now