Solved

Editing a string for numeric values

Posted on 1998-12-07
6
173 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now