Solved

Function to allow only integer values

Posted on 2002-05-07
9
269 Views
Last Modified: 2012-05-04
In a bound control - in a varchar field i want to accept only integer like 0002. so if user enters 1 i need to add 0000 in that.

Now i need to limit the user entry user should be able to enter only integer value. I tried to use the mask but it seems it is not working properly

rajni
0
Comment
Question by:rajniyadav
  • 2
  • 2
  • 2
  • +3
9 Comments
 
LVL 10

Accepted Solution

by:
Jacco earned 50 total points
ID: 6994327
What you could do is the following:

function StripLeftZeroes(const aText: string): string;
begin
  Result := aText;
  while (Length(Result) > 0) and (Result[0] = '0') do
    Delete(Result, 1, 1);
end;

function PadLeftZero(const aText: string; aLength: Integer): string;
begin
  Result := aText;
  while Length(Result) < aLength do
    Result := '0' + Result;
end;

procedure TMyForm.EditOnEnter(Sender: TObject);
begin
  Edit.Text := StripLeftZeroes(Edit.Text);
end;

procedure TMyForm.EditOnExit(Sender: TObject);
begin
  Edit.Text := PadLeftZero(Edit.Text, 4);
end;

This way when the user is in the control there are no leading zeroes and they are added when the user leaves the field.

There is one pitfall which is the "default" button of a modal form. If the ActiveControl of the form is the edit and the form is closed using the default button (i.e. save and close) the OnExit of the edit is not called.

What you can do to solve this is:

procedure TMyForm.BtnStoreClick(Sender: TObject);
begin
  ActiveControl := TWinControl(Sender); // or should it be TControl
end;

Hope this works for you. Another good alternative is to use the Orpheus component-set of TurboPower. In this set there are several very powerful mask-edit control which can do what you want as well.

(www.turbopower.com) (they are not free)

Regards Jacco
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6994699
if you use a tfield for this for bounding with a tdbedit for ex. you can use the tfield-onsettext-event (for filling up) and the onvalidate-event for check for integer

or both in the onsettext event

you could also use the onkeypress-event
of the tdbedit for ex. for validate the pressed key

meikl ;-)
0
 
LVL 4

Expert Comment

by:YodaMage
ID: 6994813
Here is my function to pad value:

function LPad(S : string; len : integer; PadChar : string): string;
begin
   Result := S;
   if len > Length(S) then
   begin
      while len > Length(Result) do
         Result := PadChar + Result;
   end;
end;

*It allows for you to pass value, length desired, and character to pad with*

Now in your app:

var
  value : String[5];
  flag  : boolean;

try
  value := strtoint(value);
  flag := True;
except
  flag := false;
  MessageDlg('Value is not valid.', mtError, [mbOK], 0);
end;
if flag then value := LPad(value, 5, '0');
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 45

Expert Comment

by:aikimark
ID: 6995503
Personally, I'd recommend a two-fold approach to arrive at zero-prefixed non-negative integer:
1. only allow "0".."9" characters in the Edit control.
2. in the lostfocus event, format the number with leading zeroes.
0
 

Author Comment

by:rajniyadav
ID: 6995749
I did it on keypress event

0
 

Author Comment

by:rajniyadav
ID: 6995751
Not a satisfactory answer.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6996173
i mentioned this above

(delete not agreed)
0
 
LVL 45

Expert Comment

by:aikimark
ID: 6996219
Assuming that the best validation occurs without code, limiting the bound control to only 0-9 characters implies that you will have a positive integer upon exit (barring other unusual behavior).

StrToInt() converts this to a number.  If the user might leave the control empty, then default to a zero.

leading-zero-pad example
Format('%.10d', [15]) will return '0000000015'
0
 
LVL 1

Expert Comment

by:pnh73
ID: 9005116
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept Answer from Jacco

Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Paul (pnh73)
EE Cleanup Volunteer
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

813 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

10 Experts available now in Live!

Get 1:1 Help Now