• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 641
  • Last Modified:

number validations OnExit edit1.text

Hi guys, this is my 1st post!:)

I just finished my 1st calculator on delphi 6 (im new on delphi!) and now i dont want to have letters or non-numbers caracters when the user OnExit'ing the edit1.text .

i have tryed :

if (edit1.text <> '1..999999')
 then
  begin
   showmessage('The caracter isnt a number, plz retry!');
   edit1.Text:='';
  end;

but it seems to not work cause it shows me the "message" everytime i enter a number!  can anyone help me plz!

another question: if im using a messageDlg, is there a way to only reading numbers?

thx a lot!
0
mediteranio
Asked:
mediteranio
1 Solution
 
ILECommented:
a plently of combination u mey use a maskedit

instead a textedit

end put in editmask something like 99999999999999

now the edit box will only accept numbers


another solution

work just when u not in id (when you run just program }

do what is writen;



   s:string;
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
s:='';
end;


procedure TForm1.Edit1Change(Sender: TObject);
var r :real;
begin
try
r:=strtofloat(edit1.text);
except
edit1.text:=s;
end;
s:=edit1.text;
end;

end.




0
 
Slick812Commented:
hello mediteranio, a good way to have control of what goes into an Edit (or memo) is the OnKeyPress Event, you get waht key has been pressed in thr Key parameter, , and then you can allow that key, change that key (to just capitals) or block that key, if you change the key to #0, it blocks that key


procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if Ord(Key) = 8 then Exit;
{#8 is the backspace key}
if (Ord(Key) < 48) or (Ord(Key) > 57) then
Key := #0;
end;
0
 
RatjeCommented:
u can use something like this :

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
  if not (Key in ['0'..'9','-',#8]) then
    if (Key in [',', '.']) then
     Key := DecimalSeparator
    else
     Key := #0 ;

end;

This way u can use either , or . for decimalseparation .



Rat
0
Independent Software Vendors: 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!

 
BLaymanCommented:
I agree with Ratje.

****Do not rely on exception checking when you can do a simple if statement.****  

Exception checking (Try Finally/Except End) is INCREDIBLY SLOW compared to one if statement.  If you get in the habit now, you'll never stop and sometime down the road you'll find some repeated routine is taking minutes when it should have been done in seconds.

Also watch for calls like StrToIntDef that do exactly the same thing as the try block just in delphi's code instead of yours.
0
 
D-MasterCommented:
onExit

if (strtoint(edit1.text) < 1) or (strtoint(edit1.text) > 999999)
then
 begin
  showmessage('The caracter isnt a number, plz retry!');
  edit1.Text:='';
  activecontrol := edit1;
 end;
0
 
D-MasterCommented:
onExit

if (strtoint(edit1.text) < 1) or (strtoint(edit1.text) > 999999)
then
 begin
  showmessage('The caracter isnt a number, plz retry!');
  edit1.Text:='';
  activecontrol := edit1;
 end;
0
 
BLaymanCommented:
ummm...
strtoint('a') = baboom
0
 
D-MasterCommented:
first use what Ratje said to make sure that the text in edit is a number
-------------------------------------------
then to make sure that the number he entered is in the range you want it to be in, use this

if (strtoint(edit1.text) < 1) or (strtoint(edit1.text) > 999999)
then
begin
 showmessage('The caracter isnt a number, plz retry!');
 edit1.Text:='';
 activecontrol := edit1;
end;
0
 
BLaymanCommented:
Ah...
0
 
mediteranioAuthor Commented:
thx a lot guys!! im sorry i had some problems with my email so i wasnt able to go check my current password lol
0
 
ChuroCommented:
try


procedure Tform1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
   if not ( Key in ['+', '-', '.', '0'..'9'] ) then
  begin
    Key := #0;
    MessageBeep(MB_ICONEXCLAMATION);
  end
  else inherited KeyPress(Key);
end;


with this code only will accept + , - , . and numbers, when try to press onother key ... BEEP !
0
 
CleanupPingCommented:
mediteranio:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Tackle projects and never again get stuck behind a technical roadblock.
Join Now