A Validating Edit Control

Value Check

Values which are syntactically correct may not meet other criteria. For example, credit card numbers apply a validation algorithm in which one of the digits (usually the low-order one) is some function of the preceding digits. One common scheme years ago would add up the digits modulo 10, then subtract the resulting value from 9, and use the resulting digit as the low-order digit. You might put range checks in place, or validate that the day of the month does not exceed the valid range for the selected month (no February 31st, for example).

In my sample program, I limit the value to have an absolute value of greater than 1.0, a positive value of <= 8192.0f, and a negative value of >= -16384.0f. How do we couple the value range check into the basic validation? The answer is that any time I get a syntactically valid number, I send a message to the parent window requesting that it validate the control. It returns, from the SendMessage, a Boolean value of TRUE or FALSE to indicate if the value is valid.

To do this, I use a user-defined message, in fact, a Registered Window Message, to notify the parent. See my essay on Message Management for more details about this. In this case, I use a static class member variable, which I declare in the class as


I initialize this in the .cpp file by doing


I react to this message by placing the following line the the MESSAGE_MAP of the parent. Note that the message request follows the magic ClassWizard comments.


I have defined the parameters of this message to be as shown:

*                                UWM_CHECK_VALUE
* Inputs:
*        LPARAM: (LPARAM)(HWND): Window handle
* Result: BOOL
*        TRUE if value is acceptable
*        FALSE if value has an error
* Effect: 
*        If the value is FALSE, the window is marked as an invalid value
*        If the value is TRUE, the window is marked as a valid value
* Notes:
*        This message is sent to the parent of the control as a consequence
*        of the EN_CHANGE notification, but only if the value is syntactically
*        correct. It may be sent at other times as well

You might also like...



Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Every language has an optimization operator. In C++ that operator is ‘//’”