[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2011-03-02
10
Medium Priority
?
165 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
9 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 672 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 24

Assisted Solution

by:jimyX
jimyX earned 664 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
 
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:Ephraim Wangoya
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:Ephraim Wangoya
Ephraim Wangoya earned 664 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 101

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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

873 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