Click here to Skip to main content
12,293,601 members (63,118 online)
Click here to Skip to main content

Stats

326.6K views
8.3K downloads
246 bookmarked
Posted

CFilterEdit: Use Regular Expressions to Filter Your Input

, 3 Oct 2011 CPOL
The definitive approach to filtering input text. Includes configurable error display.
#include "StdAfx.h"
#include "YearEdit.h"

namespace FilterEdit
{
bool CYearEdit::m_bInitialised = false;
CRegEx CYearEdit::m_RegEx;

CYearEdit::CYearEdit () :
	CBaseEdit (&m_RegEx)
{
	if (!m_bInitialised)
	{
		CString strRegEx (_T("175[3-9]|17[6-9][0-9]|1[8-9][0-9]{2}|")
			_T("[2-9][0-9]{3}"));

		m_RegEx.assign (strRegEx);
		m_bInitialised = true;
	}
}

CYearEdit::~CYearEdit ()
{
}

int CYearEdit::GetValue () const
{
	CString strValue;

	GetWindowText(strValue);
	return atoi(strValue);
}

BEGIN_MESSAGE_MAP(CYearEdit, CBaseEdit)
	ON_WM_KILLFOCUS()
END_MESSAGE_MAP()

void CYearEdit::OnKillFocus (CWnd *pNewWnd)
{
	if (GetWindowTextLength() < 4)
	{
		const int iYear = COleDateTime::GetCurrentTime().GetYear();
		CString strYear;

		strYear.Format("%d", iYear);
		SetWindowText(strYear);
	}

	CBaseEdit::OnKillFocus (pNewWnd);
}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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 22 years.

You may also be interested in...

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