Link to home
Create AccountLog in
Avatar of formadmirer
formadmirerFlag for United States of America

asked on

Validate Data Entry: keypress

Hello all. I'm trying to verify that only numbers and/or periods are entered into a text box.

I have the code for 'keypress' that validates the number entry, but I am unable to find the keycode needed to validate for a period.

Hopefully someone here knows the keycode for period?
ASKER CERTIFIED SOLUTION
Avatar of Maeros
Maeros
Flag of Canada image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of Olaf Doschke
To make a textbox a numeric textbox, just init it's value to 0.0 and you're only capable to enter numbers.

With Inputmask you might also limit the number of decimal places.

No need to use Keypress.

Bye, Olaf.
FoxPro returns 46 for point independently on the part of keyboard used.

Keycodes are listed in INKEY() help topic.

You may also simply write
? INKEY(0)
in Command Window and the result is visible immediately after a key press.

The InputMask is a good option if you have to enter fixed number of decimal places. You should also think about comma which is used in place of decimal point in many regional environments. In such case it is better to use SET("POINT") as a pattern for decimal point.

The numeric representation of SET("POINT")  is  ASC(SET("POINT")).

KeyPress handling is necessary if you want to simulate "Calculator number input".
In the Inputmask you specify a decimal point as dot, always. What the user will see at runtime depends on regional settings and/or SET("POINT"), so that's not to worry about, too.

The simplest solution is to preset a numerical value, and voilá, you can only enter numerical values, the value type itself will then be numeric, too.

The same goes by presetting a date, if you want a user to only enter dates.

Another solution is to bind to a field of the wanted type, no matter if to a DBF or a cursor generated just for that matter.

Bye, Olaf.
I have to correct my post:

I did not mean the dot in input mask but the dot and/or comma code processed in KeyPress event. Some regional numeric keyboards generate comma when you press dot. SET POINT also inherits this value when you SET SYSFORMATS ON etc.

Thus I am always accepting both comma and dot in numeric fields data entry in our country.

The InputMask does not allow to enter variable number of decimal digits in numeric field which could be a problem (in a few cases, of course).

BTW, almost every UI crashes or reports a bug when you enter NULL value... Fortunately users don't know how to do it.
OK, Pavel, I didn't thought about the decimal point key in the numpad, this is causing different keycodes depending on regional settings, that's true.

In regard of NULL: ON KEY LABEL CTRL+0 *
prevents a user knowing that "Hotkey" to NULL a textbox. It's something you should do always, also to prevent NULL as entry in a login password textbox or other data field entries.

Bye, Olaf.
Avatar of formadmirer

ASKER

I was looking for a way to validate a version number, which may have up to four periods, so I can't make use of most of the typical ways of validating the input.

In this case this was exactly what I was looking for and did the trick nicely. And I hadn't even thought about the the period/decimal key on the numpad, so extra thanks for that.
Just one note (warning) to the solution awarded:

You should validate what's written down. Not everything is true and not everything will work as you would suppose... The version number could contain some unwanted characters after the implementation.
Version number like INPUTMASK "99.99.99.99"? C'mon.

Bye, Olaf.

PS: To be more detailed: The . in an inputmask will only be the decimal point, which may or may not be a comma instead, if the VALUE of the textbox is numeric. if you set the textbox value to "01 00 00 00" and use that inputmask, it will be displayed as 01.00.00.00, no matter if SET("POINT") is a comma or dot.