?
Solved

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

Posted on 2011-03-02
10
Medium Priority
?
157 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
[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
  • 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 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

765 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