Click here to Skip to main content
11,720,557 members (55,576 online)
Click here to Skip to main content

CFilterEdit: Use Regular Expressions to Filter Your Input

, 3 Oct 2011 CPOL 319.3K 8.2K 245
Rate this:
Please Sign up or sign in to vote.
The definitive approach to filtering input text. Includes configurable error display.
Screenshot - FilterEdit.png

Introduction

Ever since computers were invented, data validation has been an important concern. When it comes to a user interface, the fastest way to validate data is whilst it is being input. Strange, then, that there is no standard Windows edit control that tackles this problem for any kind of data.

This validating edit control aims to provide the ultimate Framework for data validation, no matter how complex the data you are attempting to input is.

Design Decisions

  • Validation must be clean and reliable
  • The control should be usable exactly like CEdit
  • Only a handful of visual effects will be included as standard
  • 'WM_KILLFOCUS is the wrong time to do field validation'
  • Auto-formatting should be easy to add from a derived class
  • FilterEdit must stay as simple as possible and not get too bloated!

Intercepted Windows Messages

The following Windows messages are trapped for validation purposes:

  • EM_REPLACESEL
  • WM_CLEAR
  • WM_CHAR
  • WM_CUT
  • WM_KEYDOWN
  • WM_KEYUP
  • WM_KILLFOCUS
  • WM_PASTE
  • WM_SETFOCUS
  • WM_SETTEXT

... and these are trapped to perform the visual effects:

  • WM_CTLCOLOR
  • WM_PAINT

Windows Message Overrides for Validation

CBaseEdit::OnChar

WM_CHAR is the Windows message that every validating edit control ever written for Windows must have trapped. This is where a single character can be checked to see if the control will accept it or not.

CBaseEdit::OnKeyDown

WM_KEYDOWN is where the Delete key, Ctrl-X, Ctrl-C and Ctrl-V are trapped.

CBaseEdit::OnKillFocus

Again, WM_KILLFOCUS is a very popular message to trap. However, rather than trying to set the focus back to the control in the event that the input is incomplete, we just flag the error and allow the focus to switch normally.

CBaseEdit::OnSetFocus

WM_SETFOCUS is trapped simply so that we can set any colours we need to and display a tooltip if required.

CBaseEdit::WindowProc

This is where the rest of the Windows messages we are interested in are processed.

How to Derive Your Own Custom Control

Here are the four obvious things you might want to do:

  • Override SemanticCheck

    See the CUIntRangeEdit example to see how this works.

  • Trap WM_CHAR yourself

    By trapping characters yourself, you can automatically format input and perform semantic validation as the user types. Refer to the CDateTimeEdit example to see how this works.

  • Trap WM_KILLFOCUS yourself

    You may want to perform extra formatting when the user leaves the control. Again, see the CDateTimeEdit example. Don't forget to call CBaseEdit::OnKillFocus if you do this!

  • Override SyntaxCheck

    This is if you want to pre-process the string before running the syntax checking. See CDateTimeEdit for an example.

Example Controls Included in the ZIP

  • CCurrencyEdit
  • CDateTimeEdit
  • CFloatEdit
  • CIntEdit
  • CUIntEdit
  • CUIntRangeEdit
  • CSpin

Tooltip Support

You might want to explain the data format for your controls at runtime. For this reason, Balloon Help support is included. To include this support, you must call CreateToolTip:

BOOL CEditTestDlg::OnInitDialog()
{
    CDialog::OnInitDialog ();
    ...
    m_DateEdit.CreateToolTip (this, _T("Tooltip text here"));
    ...
}

The C++ Standard Regular Expression Library

The boost::regex library has been accepted into the C++ Standard Library. Due to its partial match support, the library is also ideal for this control. Get the Boost library here.

TO DO

  • Add more demo controls
  • Tutorial for deriving controls

Feedback

All feedback is most welcome!

Revision History

  • 1.0.0.0 - 30th July, 2004
    • Original version posted
  • 1.0.3.5 - 8th August, 2007
    • BUG FIX: Now using GetKeyState() in CBaseEdit::OnKeyDown()
  • 1.0.3.6 - 22nd August, 2007
    • Now allows user to delete the entire contents of an edit control, regardless of the regex
  • 1.0.3.7 - 14th January, 2008
    • Changed CDateEdit - now CDateTimeEdit
  • 1.0.3.8 - 25th February, 2008
    • Fixed double paste bug
  • 1.0.3.9 - 13th June, 2008
    • Added bool CDateTimeEdit::GetTimeStamp (SQL_TIMESTAMP_STRUCT *pTS)
    • Filter out Ctrl-X, Ctrl-C and Ctrl-V in CBaseEdit::ValidateChar()
  • 1.0.4.0 - 8th August, 2008
    • Display tooltip right at the bottom of the edit control to avoid painting overwriting spin control border!
  • 1.0.4.1 - 15th September, 2008
    • Use system colours as defaults, support boost::gregorian::date
  • 1.0.4.2 - 15th October, 2008
    • Dynamic calendar dialog and bitmap for CDateTimeEdit (no resource file entries required now)
  • 1.0.4.3 - 7th November, 2008
    • Fixed assertions in CDateTimeEdit::WindowProc
  • 1.0.4.4 - 5th December, 2008
    • Added boost posix_time support to CDateTimeEdit
  • 1.0.4.5 - 9th January, 2009
    • Allow WM_SETTEXT to set an empty string even if regex forbids it
  • 1.0.4.6 - 1st May, 2009
    • Interface change for Date/Time Get functions
  • 1.0.4.7 - 7th September, 2009
    • Bug fix to CDateTimeEdit::SyntaxCheck
  • 1.0.4.8 - 19th May, 2010
    • Adds unsigned support to FloatEdit
  • 1.0.5.0 - 7th October, 2010
    • This version extends CFloatEdit so that you can specify the number of digits for the whole part
  • 1.0.5.1 - 25th October, 2010
    • This version fixes a bug in CDateTimeEdit::SetTime()
  • 1.0.5.2 - 26th January, 2011
    • This version adds an HHMM mode for times
  • 1.0.5.3 - 17th February, 2011
    • This version adds the BS_DEFPUSHBUTTON style to the calendar button in CDateTimeEdit
  • 1.0.5.4 - 21st February, 2011
    • Changed the date ordering for get/set functions in CDateTimEdit to bring it in line with COleDateTime
  • 1.0.5.5 - 8th April, 2011
    • This update fixes range checking in CDateTimeEdit::SetTime()
  • 1.0.5.6 - 12th September, 2011
    • This version adds SetMin() and SetMax() to CDateTimeEdit and fixes some bugs
  • 1.0.5.7 - 3rd October, 2011
    • This version fixes a bug in CDateTimeEdit::SemanticCheck()

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Ben Hanson
Software Developer (Senior)
United Kingdom United Kingdom
I started programming in 1983 using Sinclair BASIC, then moving on to Z80 machine code and assembler. In 1988 I programmed 68000 assembler on the ATARI ST and it was 1990 when I started my degree in Computing Systems where I learnt Pascal, C and C++ as well as various academic programming languages (ML, LISP etc.)

I have been developing commercial software for Windows using C++ for 15 years.

You may also be interested in...

Comments and Discussions

 
GeneralMessage Maps in Derived Classes Pin
Ben Hanson10-Jun-05 23:16
memberBen Hanson10-Jun-05 23:16 
GeneralVisual Studio .Net 2002 Pin
defender564-May-05 5:42
memberdefender564-May-05 5:42 
General1.0.2.9 Pin
Ben Hanson4-May-05 7:00
memberBen Hanson4-May-05 7:00 
GeneralRe: 1.0.2.9 Pin
defender564-May-05 8:09
memberdefender564-May-05 8:09 
QuestionThis doesn't work in VC++ 6? Pin
Anonymous1-May-05 10:29
sussAnonymous1-May-05 10:29 
AnswerRe: This doesn't work in VC++ 6? Pin
Christian Graus1-May-05 12:43
memberChristian Graus1-May-05 12:43 
AnswerFix Pin
Ben Hanson1-May-05 21:58
memberBen Hanson1-May-05 21:58 
QuestionWhere is header File? Pin
Agha Khan8-Apr-05 3:18
memberAgha Khan8-Apr-05 3:18 
GeneralGeneric RangeEdit Pin
yuasa7-Apr-05 8:29
memberyuasa7-Apr-05 8:29 
GeneralRe: Generic RangeEdit Pin
Ben Hanson7-Apr-05 20:50
memberBen Hanson7-Apr-05 20:50 
Generalcall DrawFrame to remove wavy line Pin
yuasa7-Apr-05 8:09
memberyuasa7-Apr-05 8:09 
GeneralRe: call DrawFrame to remove wavy line Pin
Ben Hanson7-Apr-05 20:33
memberBen Hanson7-Apr-05 20:33 
GeneralRe: call DrawFrame to remove wavy line Pin
yuasa7-Apr-05 21:10
memberyuasa7-Apr-05 21:10 
GeneralRe: call DrawFrame to remove wavy line Pin
Ben Hanson7-Apr-05 21:49
memberBen Hanson7-Apr-05 21:49 
GeneralRe: call DrawFrame to remove wavy line Pin
yuasa7-Apr-05 21:55
memberyuasa7-Apr-05 21:55 
General1.0.2.7 Posted Pin
Ben Hanson8-Apr-05 2:42
memberBen Hanson8-Apr-05 2:42 
GeneralApplication crash Pin
Fanwy23-Mar-05 14:32
memberFanwy23-Mar-05 14:32 
General1.0.2.4 Posted Pin
Ben Hanson23-Mar-05 20:37
memberBen Hanson23-Mar-05 20:37 
GeneralSTLport Pin
martyCZ22-Mar-05 20:55
membermartyCZ22-Mar-05 20:55 
GeneralRe: STLport Pin
martyCZ23-Mar-05 3:32
membermartyCZ23-Mar-05 3:32 
GeneralAnother one bit to tooltip Pin
oem22-Mar-05 0:41
memberoem22-Mar-05 0:41 
General1.0.2.6 Posted Pin
Ben Hanson22-Mar-05 0:51
memberBen Hanson22-Mar-05 0:51 
GeneralFilterEdit design (tooltip member) Pin
mproper12314-Mar-05 20:19
sussmproper12314-Mar-05 20:19 
GeneralRe: FilterEdit design (tooltip member) Pin
Ben Hanson20-Mar-05 0:37
memberBen Hanson20-Mar-05 0:37 
GeneralConsolidation Pin
Ben Hanson10-Mar-05 1:05
memberBen Hanson10-Mar-05 1:05 
GeneralRe: Consolidation Pin
Vivic11-Mar-05 14:15
memberVivic11-Mar-05 14:15 
GeneralRe: Consolidation Pin
Ben Hanson12-Mar-05 3:59
memberBen Hanson12-Mar-05 3:59 
GeneralToolTip Suggestion Pin
Eldon Zacek3-Mar-05 8:41
memberEldon Zacek3-Mar-05 8:41 
General1.0.2.1 Posted Pin
Ben Hanson3-Mar-05 20:23
memberBen Hanson3-Mar-05 20:23 
GeneralAnother form of validation.... Pin
Vivic24-Feb-05 23:25
memberVivic24-Feb-05 23:25 
GeneralRe: Another form of validation.... Pin
Ben Hanson25-Feb-05 0:03
memberBen Hanson25-Feb-05 0:03 
GeneralRe: Another form of validation.... Pin
Vivic3-Mar-05 16:33
memberVivic3-Mar-05 16:33 
GeneralRe: Another form of validation.... Pin
Ben Hanson3-Mar-05 20:28
memberBen Hanson3-Mar-05 20:28 
QuestionJust a question, did you know ? Pin
Kochise23-Feb-05 23:41
memberKochise23-Feb-05 23:41 
AnswerRe: Just a question, did you know ? Pin
Ben Hanson23-Feb-05 23:54
memberBen Hanson23-Feb-05 23:54 
QuestionGreat control idea! Range checking? Pin
Alan Moore19-Feb-05 9:55
memberAlan Moore19-Feb-05 9:55 
AnswerRe: Great control idea! Range checking? Pin
Ben Hanson20-Feb-05 20:51
memberBen Hanson20-Feb-05 20:51 
GeneralRe: Great control idea! Range checking? Pin
Alan Moore21-Feb-05 2:54
memberAlan Moore21-Feb-05 2:54 
GeneralRe: Great control idea! Range checking? Pin
Ben Hanson21-Feb-05 3:15
memberBen Hanson21-Feb-05 3:15 
GeneralError Message? Pin
Alan Moore21-Feb-05 5:14
memberAlan Moore21-Feb-05 5:14 
GeneralRe: Error Message? Pin
Ben Hanson21-Feb-05 5:54
memberBen Hanson21-Feb-05 5:54 
GeneralRe: Error Message? Pin
Alan Moore21-Feb-05 6:03
memberAlan Moore21-Feb-05 6:03 
General1.0.1.6 Posted Pin
Ben Hanson22-Feb-05 8:30
memberBen Hanson22-Feb-05 8:30 
Answer1.0.1.5 Posted Pin
Ben Hanson21-Feb-05 5:48
memberBen Hanson21-Feb-05 5:48 
GeneralVery useful and nearly perfect Pin
wzett19-Jan-05 22:12
memberwzett19-Jan-05 22:12 
General1.0.1.1 Posted Pin
Ben Hanson20-Jan-05 1:19
memberBen Hanson20-Jan-05 1:19 
GeneralBlue static of death ? Pin
Kochise26-Jan-05 5:56
memberKochise26-Jan-05 5:56 
GeneralSetBackgroundColourOK Usage Pin
Ben Hanson26-Jan-05 20:40
memberBen Hanson26-Jan-05 20:40 
GeneralOups... Pin
Kochise26-Jan-05 21:11
memberKochise26-Jan-05 21:11 
General1.0.1.4 Posted Pin
Ben Hanson26-Jan-05 22:11
memberBen Hanson26-Jan-05 22:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150901.1 | Last Updated 3 Oct 2011
Article Copyright 2004 by Ben Hanson
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid