Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search for 0 (zero) and O 2

Status
Not open for further replies.

jonek

Programmer
Dec 28, 2011
2
GB
May I tap your collective genius.

I presently run a search for a string using the Pos command. However, users have been known to enter 0 when they mean O and vice-versa.

Is there an efficient search method for returning a result, say 01230, if 01230 or O1230 or 0123O or O123O are typed as the search criteria?

Hope that makes sense. Thanks very much for your time.

Jon
 
If the only legitimate input is going to be only numeric, you can trap the keypress event and check to see if the key is a non-numeric key and reject the input and beep if not; Alternate scenario is to swap non numeric with numeric. something like so:

Code:
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
VAR
  NumChars: set of '1'..'0';
begin
  if Key not in NumChars
  begin
    char:=#0; //  Stop further processing of keystroke
    Beep;     //  sound alert
  end
end;

This forces the user to enter only numeric keys and beeps if not.

NOTE: This code only works for base 10 integers.
 
+1 for Prattaratt's comment, validate inputs first!

if this is not an option you can replace the 'O' chars with '0' with one line of code:

Code:
function SanitizeInput(Input : String) : String;
begin
 Result := StringReplace(Input, 'O', '0', [rfReplaceAll]);
end;

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Just wanted to thank Prattaratt and whosrdaddy who collectively gave me exactly what I needed.

I should have been clearer in original post - both O and 0 are valid search characters. But the final code became:

Code:
  EditAsZeros := ReplaceText(SOSearchEdit.Text, 'O', '0');
  CONAsZeros := ReplaceText(VOrdView.FieldByName('CustOrdNo').Text, 'O', '0');
  if (Pos(EditAsZeros, CONAsZeros) > 0) then
  ...

Job done. Thanks ever so much.

Jon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top