Solved

Editing a string for numeric values

Posted on 1998-12-07
6
175 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
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi XE2 application frozen on Windows 10 10 312
Best Firemonkey component pack 1 105
Reconfigure Delphi Install? 2 51
how to change, disabled button color FMX ? 1 33
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.

803 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