Solved

Function to allow only integer values

Posted on 2002-05-07
9
275 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 46

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 46

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month10 days, 17 hours left to enroll

628 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