7

So i have code already. But when it runs it doesn't allow the backspace key, I need it to allow the backspace key and remove the space bar as I don't want spaces.

procedure TForm1.AEditAKeyPress(Sender: TObject; var Key: Char); var s:string; begin s := ('1 2 3 4 5 6 7 8 9 0 .'); //Add chars you want to allow if pos(key,s) =0 then begin Key:=#0; showmessage('Invalid Char'); end; 

Need help thanks :D

3
  • @DavidHeffernan, TMaskEdit usually is used for fixed length of the input text Commented Jul 31, 2012 at 9:16
  • What happens if the user pastes text into the edit box? Commented Jul 31, 2012 at 21:06
  • @DavidHeffernan, just tested in TNumberEditEh from EhLib. All went ok. It handle properly Pasting, Tab, Esc, Decimal, Thousand Separator etc.. Microsoft use something similar in a lot of places including Windows, Office, etc.. Commented Aug 1, 2012 at 7:03

3 Answers 3

9

Note the comment that's already in your code:

procedure TForm1.AEditKeyPress(Sender: TObject; var Key: Char); var s:string; begin s := ('1234567890.'#8); //Add chars you want to allow if pos(key,s) =0 then begin Key:=#0; showmessage('Invalid Char'); end; end; 
Sign up to request clarification or add additional context in comments.

Comments

5

It's better to put your allow keys in a set as a constant (speed, optimization):

Updated #2 Allow only one decimal char and handling properly DecimalSeparator.

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char); const Backspace = #8; AllowKeys: set of Char = ['0'..'9', Backspace]; begin if Key = '.' then Key := DecimalSeparator; if not ((Key in AllowKeys) or (Key = DecimalSeparator) and (Pos(Key, Edit1.Text) = 0)) then begin ShowMessage('Invalid key: ' + Key); Key := #0; end; end; 

For better results take a look at TNumericEdit components included in DevExpress, JVCL, EhLib, RxLib and many other libraries.

9 Comments

Note: ShowMessage here is just a demo. From an ergonomic UI point of view, bothering user each time when an invalid Char is input is a disaster :) He need to press [OK] each time. It's better to play a beep error, or write a message in a console window / notification area.
It's hard to imagine the user being able to deliver key presses at a rate where performance of Pos mattered. And I'm not sure that a set really would be faster than Pos anyway.
That's utter nonsense. The number of CPU cycles that it takes to get from the key being pressed to the message arriving in your apps message queue is many orders of magnitude greater than the time taken to call Pos.
Yeah, just made a more exact test ~ 18 times faster (XE2). I just point to anyone to learn to write efficient code. FastCode library is an example and most part of it was included in RAD Studio. Speed counts. Oh... where are '90 days, ultra-optimisation, asm etc...? ;)
Personally I'd use a set here, assuming that we were working with AnsiChar and not Char. But I'd do it because the code reads so much better. The performance issue is not pertinent in a KeyPress handler. It's much more important to get readability and clarity for code like this. Optimisation comes later and is applied to bottlenecks. If you optimised this code, say by turning it into asm then that would be a dreadful idea because you would now have code that still takes effectively zero time to run, but now it is unreadable!
|
1

Using psychic powers, I predict that you're trying to validate a floating point value.

You can use the OnExit event of the control or the Ok/Save facility of your form to check the correct format like this:

procedure TForm1.Edit1Exit(Sender: TObject); var Value: Double; begin if not TryStrToFloat(Edit1.Text, Value) then begin // Show a message, make Edit1.Text red, disable functionality, etc. end; end; 

This code assumes that you want to use a locale specific decimal separator.

If you only want to allow a '.' you can pass a TFormatSettings record as the third parameter to TryStrToFloat.

5 Comments

+1 I think validating when the user is ready to move on is much better than ignoring invalid characters. What's more this caters for decimal separators, scientific notation, pasting values etc.
I think it's better to prevent in time a problem, than informing a user that something is going wrong after, OnExit-ing. There is a lot of TNumericEdit components that allow pasting, scientific notations, etc...
@marcodor The downside of blocking input of certain keys is that the user thinks the keyboard is broken. Blocking keys doesn't inform the user of anything unless you show some error text. Also you still need to perform extra validation. What if the user puts in two decimal seps? Or the E of scientific notation in the wrong place. Validating char by char is hard to get right, and for what gain?
@DavidHeffernan I don't think so. Take a look: s18.postimage.org/gy4zry055/Untitled.png As I sad there is a lot of smart numeric input components, suporting all numeric input features.
@marcodor I don't agree with you but to their own opinion.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.