[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 755
  • Last Modified:

edit box only numbers allow

Hi I'd to know how I could only allow numbers to be added and nothing else in the edit box

just numbers like 123456789.0000000
0
eNarc
Asked:
eNarc
  • 2
1 Solution
 
Richard QuadlingSenior Software DeverloperCommented:
I would have thought an edit mask would have worked, but I don't know the exact syntax.

But I think edit masks are for fixed length content.

http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_23700739.html is a similar question. I think the answer could be expanded to allow digits and '.' quite easily.
0
 
Kamaraj SubramanianApplication Support AnalystCommented:

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
  // #8 is Backspace
  if not (Key in [#8, '0'..'9']) then begin
    ShowMessage('Invalid key');
    // Discard the key
    Key := #0;
  end;
end;

Open in new window

0
 
Kamaraj SubramanianApplication Support AnalystCommented:
well, i didnt notice that you need . (dot) also

try this
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
  if not (Key in [#8, '0'..'9', DecimalSeparator]) then begin
    ShowMessage('Invalid key: ' + Key);
    Key := #0;
  end
  else if (Key = DecimalSeparator) and
          (Pos(Key, Edit1.Text) > 0) then begin
    ShowMessage('Invalid Key: twice ' + Key);
    Key := #0;
  end;
end;

Open in new window

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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