Click here to Skip to main content
Click here to Skip to main content

Filtered Textbox

By , 9 Oct 2005
Rate this:
Please Sign up or sign in to vote.
<!-- Article Starts - DO NOT ADD HTML/BODY START TAGS-->


Have you ever needed to create a textbox that only allows you to enter numbers? Or letters? Or strings that don't start with a number? C# 2.0 addresses this point to a certain extent with the masked textbox. However, this does not help if you need to use more complex rules to define the validation.

To this end, I created a FilteredTextBox, that uses a regular expression filter to define what can be entered in the textbox. If it cannot be entered, it will not appear (unlike a post-validation, that would create an error message, or cleanup and remove invalid characters after)

Using the Code

To use the compiled componant, enter a regular expression that for any acceptable string in the textbox will create a match in the Filter property.

Adding Sound

I excluded this to enable the code to work with .NET 1.1 However, if you are using 2.0, or can do something similar in 1.1, it may be a good idea to provide audio feedback when an invalid character is typed. I did this by adding a boolean property of ShouldPlaySound and modiftying the code to match below:
if (!regexFilter.IsMatch(inputText))
    if (PlayErrorSound)
    e.Handled = true;


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author


United Kingdom United Kingdom
No Biography provided

Comments and Discussions

Generaloptimized for filtering every char Pinmemberjopomo20-Nov-06 4:35 
GeneralBug in filteredTextBox_KeyPress PinmemberPerry211-Apr-06 13:10 
GeneralWM_PASTE Pinsusstonyt9-Oct-05 20:27 
GeneralRe: WM_PASTE Pinmember[ICR]9-Oct-05 21:20 
GeneralUmm... PinmemberThe_Mega_ZZTer9-Oct-05 7:45 
GeneralRe: Umm... Pinmember[ICR]9-Oct-05 7:56 
Its a regular expression applied to the text overall as a whole, not just the character entered. I do, though, see your point. I'm pretty sure it can still be covered with a regex though, but my regex is quite terrible, so I'm not sure how it would be done.
It does solve many situations that a masked textbox doesn't really fit though. Especialy the given example, such as only allowing numbers, letters etc.
It isn't a Microsoft policy of post-validating. Almost all Microsoft, and big name apps I've seen use on the fly validation such as this. The "Upper" and "Lowercase" filters on TextBox's are another example of this. Even the masked textbox's are on the fly validation, just in alot stricter environment, which allows them to have prompts as to what characters you can enter.
-- modified at 14:03 Sunday 9th October, 2005
GeneralRe: Umm... PinmemberThe_Mega_ZZTer9-Oct-05 7:59 
GeneralRe: Umm... Pinmember[ICR]9-Oct-05 8:06 
GeneralRe: Umm... Pinmember[ICR]9-Oct-05 11:48 

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 | Mobile
Web01 | 2.8.140415.2 | Last Updated 9 Oct 2005
Article Copyright 2005 by [ICR]
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid