Click here to Skip to main content
6,822,613 members and growing! (15,672 online)
Email Password   helpLost your password?
Desktop Development » Edit Controls » Beginners     Intermediate License: The Code Project Open License (CPOL)

CFilterEdit: Use Regular Expressions to Filter Your Input

By Ben Hanson

The definitive approach to filtering input text. Includes configurable error display.
C++ (VC7.1), Windows (Win2K, WinXP), Visual-Studio (VS.NET2003), MFC, Dev
Revision:5 (See All)
Posted:29 Jul 2004
Updated:8 Sep 2009
Views:191,324
Bookmarked:171 times
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
45 votes for this article.
Popularity: 7.56 Rating: 4.58 out of 5
1 vote, 2.2%
1
1 vote, 2.2%
2
5 votes, 11.1%
3
2 votes, 4.4%
4
36 votes, 80.0%
5
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

License

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

About the Author

Ben Hanson


Member
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.
Occupation: Software Developer (Senior)
Location: United Kingdom United Kingdom

Other popular Edit Controls articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 112 (Total in Forum: 112) (Refresh)FirstPrevNext
GeneralHow to use class FloatEdit Pinmemberbkelly133:35 22 Jun '08  
GeneralRe: How to use class FloatEdit PinmemberBen Hanson9:52 24 Jun '08  
General Pinmemberbkelly137:21 21 Jun '08  
GeneralRe: PinmemberBen Hanson9:34 24 Jun '08  
GeneralThis HORSE IS DEAD Jim... PinmemberHeywood6:58 16 Jan '08  
GeneralRe: This HORSE IS DEAD Jim... Pinmemberyafan7:32 16 Jan '08  
GeneralRe: This HORSE IS DEAD Jim... PinmemberMihai Maerean20:07 20 Oct '08  
QuestionValidating ALT characters Pinmembernarlad7:17 2 Oct '07  
AnswerRe: Validating ALT characters PinmemberBen Hanson3:07 10 Oct '07  
GeneralValidate URL with regex Pinmemberdressman198110:30 12 Jun '07  
AnswerRe: Validate URL with regex PinmemberBen Hanson5:30 13 Jun '07  
GeneralRe: Validate URL with regex Pinmemberdressman19815:43 14 Jun '07  
AnswerRe: Validate URL with regex PinmemberBen Hanson23:01 14 Jun '07  
GeneralRe: Validate URL with regex Pinmemberdressman19810:02 15 Jun '07  
AnswerRe: Validate URL with regex PinmemberBen Hanson6:32 15 Jun '07  
GeneralRe: Validate URL with regex PinmemberTheBlindSquirrel13:15 22 Aug '07  
GeneralBug with XP-Style / Manifest-file Pinmemberdressman19815:05 23 May '07  
GeneralRe: Bug with XP-Style / Manifest-file PinmemberBen Hanson8:30 26 May '07  
GeneralRe: Bug with XP-Style / Manifest-file Pinmemberdressman19818:22 28 May '07  
GeneralBeating a dead horse PinmemberHeywood5:42 22 Dec '06  
GeneralRe: Beating a dead horse PinmemberChris Conn6:52 15 Feb '07  
GeneralValidate regex? Pinmemberare_all_nicks_taken_or_what5:10 8 Jan '06  
GeneralRe: Validate regex? [modified] PinmemberBen Hanson22:34 8 Jan '06  
GeneralRe: Validate regex? Pinmemberare_all_nicks_taken_or_what6:47 9 Jan '06  
QuestionBug with paste PinmemberFirejano3:51 3 Jan '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

PermaLink | Privacy | Terms of Use
Last Updated: 8 Sep 2009
Editor: Deeksha Shenoy
Copyright 2004 by Ben Hanson
Everything else Copyright © CodeProject, 1999-2010
Web18 | Advertise on the Code Project