Solved

Function to allow only integer values

Posted on 2002-05-07
9
267 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

757 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

20 Experts available now in Live!

Get 1:1 Help Now