Solved

Function to allow only integer values

Posted on 2002-05-07
9
268 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to fill array with TArray.Create? 14 79
Virtuailstring tree compare node issue 14 105
QRReport  TQrmemo vertical stretching 1 42
Base1 Encode/Decode 3 68
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…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

863 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

23 Experts available now in Live!

Get 1:1 Help Now