Solved

Editing a string for numeric values

Posted on 1998-12-07
6
181 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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 antispam), the admini…

739 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