Solved

Function to allow only integer values

Posted on 2002-05-07
9
270 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi XE10, MySQL Query 4 164
Best Firemonkey component pack 1 119
Dev Express grid collapse 2 45
Twebbrowser in Fire monkey android scrolling to bottom 1 44
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…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

809 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