[Lazarus] floating point mask

David Emerson dle3ab at angelbase.com
Mon Dec 7 18:49:36 CET 2009


> You can use any input component, and check e.g. in OnKeyPress whether 
> the resulting string would be valid. If not, reject the input
> character. 
> 
> You can use any regex library for the check, but IMO that's overkill 
> with a single constant format. In your case I'd use the conversion 
> function itself, e.g. Val(), to determine whether the input is valid.

This has the disadvantage of sometimes making it difficult to type 
things naturally. e.g. if I started by typing "-" that could be 
rejected, since I haven't yet input a number to make negative.

For this reason I much prefer to have some kind of visible indication as 
to whether an input is presently valid: show the user whether their 
input is presently in a good state, but don't restrict the input in 
that way. One must do some validation later on, but that should 
probably be done anyway.

Of course, if you have a set of all the valid characters 
['-', '.', '0'..'9', 'e', 'E', '+'], it's not unreasonable to restrict 
characters outside that set. But make sure the set is complete-- at 
first I overlooked "e+" which is valid for val (string, real)

~D.





More information about the Lazarus mailing list