Solved

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

Posted on 2011-03-02
10
152 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
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…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

910 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

22 Experts available now in Live!

Get 1:1 Help Now