Solved

Editing a string for numeric values

Posted on 1998-12-07
6
179 Views
Last Modified: 2012-05-04
How do you check if a string consist of numerics?
For example:
SSN field is a string.
SSN Field...i want to make sure that all numerics are entered.

How would i edit for that?

0
Comment
Question by:waipahu
[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
6 Comments
 
LVL 17

Expert Comment

by:inthe
ID: 1349782
hi,
if ssnfeild is a edit box do this on the keypress event.

procedure TForm1.SSNFieldKeyPress(Sender: TObject; var Key: Char);
const validchars : set of char = ['0'..'9'];
begin
If not(key in validchars) then key:=#0;
end;

Barry
0
 

Author Comment

by:waipahu
ID: 1349783
No...ssn field is coming from a dbase file.
I need to check for valid SSNs in my dbase file.
I am writing a clean up program to edit for valid SSNs in the file.
The SSN field is character 13.

Thanks for your help!

0
 
LVL 1

Accepted Solution

by:
Gerhard100198 earned 10 total points
ID: 1349784
Try the following function. If the string can be converted to a numeric then the function will return true else false. Just remember that if the string contains any formatting characters, such as 1,000,000.00 you will haver to strip out the commas (,) first else thefunction will also return a FALSE.

function TestForNum(TheString: String): Boolean;
var
 x: Real;
begin
  try
    x := StrToInt(TheString);
    Result := TRUE;
  except
    Result := FALSE;
  end;
end;
0
Technology Partners: 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 27

Expert Comment

by:kretzschmar
ID: 1349785
hi inthe,

i become a dejavu

hi waipahu,

a other method to check

Function IsNumeric(S : String) : Boolean;
var
  ok : Boolean;
  i : Integer;
begin
  ok := true;
  for i := 1 to length(s) do
    if not(s[i] in ['0'..'9']) then ok := false;
  IsNumeric := Ok;
end;

meikl
0
 
LVL 1

Expert Comment

by:BlackDeath
ID: 1349786
hi all.

waipahu:

you can also use

function IsNumeric(SSN: string): Boolean;
var
  lValue: Longint;
  iCode: Integer;
begin
  Val(SSN, lValue, iCode);
  if iCode = 0 then
    Result := True
  else
    Result := False;
end;

by using Val, you can determine the index of the non-numeric character (if any); it is stored in iCode i.e. if iCode = 0, there ain't no non-numeric characters.

you could expand the function in the else-branch to handle this.

regards,

Black Death.
0
 

Author Comment

by:waipahu
ID: 1349787
Thanks so much!!!
I really appreciate everybodys help.
I am using your code!
thanks!

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

740 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