Solved

Editing a string for numeric values

Posted on 1998-12-07
6
182 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
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 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

Independent Software Vendors: 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!

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

729 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