Number in Edit fields - check :)

alenturuskovic 0 Tallied Votes 169 Views Share

Procedure which checks the number of eating properly entered in
edit box with a comma

/* 	
Procedure which checks the number of eating properly entered in 
edit box with a comma

Declaration in public
Provjeribroj Procedure (Sender: savings); 

Call procedures for example Edit1 field
provjeribroj (Edit1);
*/

procedure TForm1.provjeribroj(Sender : TEdit);
Var i,br,st: integer;
s : String;
priv : String;
begin
      br := 0;
      st:= Sender.SelStart;
      s := Sender.Text;
if  copy(s,1,1) =  DecimalSeparator then
begin
Sender.Text := copy(s,2,length(s)) ;
Sender.SelStart :=st;
exit;
end;
      for i := 1 to length(s) do
        begin
            if s[i] in ['0'..'9',DecimalSeparator] then
              begin
                      if  (s[i] = DecimalSeparator) then
                      begin
                          br:=br+1;
                      end;

                    if not(( br>1) and (s[i] = DecimalSeparator)) then
                      begin
                      priv := priv + s[i];
                      end
                      else
                      st:=st-1;

              end
              else
              begin
                 st:=st-1;
              end

           end;
     Sender.Text := priv;
     Sender.SelStart :=st;

end;
alenturuskovic 0 Newbie Poster

I hope that this code will help someone because I had problems with numeric fieldsu

commented: ok +0
remagin 0 Newbie Poster

hrap mghanap ng code

.. haha!

Wolfgan 17 Junior Poster

I think my code is much smaller. Less code = fewer bugs.
Сan just not give enter other characters. Put on the shape of TEdit. Create his handler OnKeyPress and paste the following code:

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
  if Key in ['-','0','1','2','3','4','5','6','7','8','9',#8,DecimalSeparator] then
  begin
     if (Key = DecimalSeparator) and 
        (Pos(DecimalSeparator,(Sender as TEdit).Text) > 0) then
         Key := #0;
  end else
      Key := #0;
end;

You can assign one event to several TEdit.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.