Restricting User Input

Introduction

This article describes an approach to restricting the user's input to letters only, numbers only, letters or numbers only, and special characters only.

Figure 1.  Restricting User Input into Text Box Controls

Getting Started.

There is a single Win Forms application included with this download.  You may open the project and examine the project if you wish; however, the code is quite simple and will be described in the following sections of this document.  All examples were written using Visual Studio 2005 and are in C#; the same code could also be used in earlier versions of Visual Studio.

Code Example:  Restricting User Input.

The application contains a single form; the form contains four text box controls and each text box is labeled to describe the required input for the text box.  There is also a single button control which is used to terminate the application.

In order to evaluate the user's entries into the text box and to prevent the entry of restricted characters, a key press event handler is added for each of the text box controls.  The keyboard inputs submitted to the text box control are intercepted and evaluated prior to writing any information into the text box.

The first text box is restricted to allow only letters to be entered into the text box.  In order to perform this input filtering, the key press event argument is evaluated using the character structure's "IsLetter" function to determine whether or not the value keyed in is in fact a letter.  In this instance, if the character is not a letter, the key press event is disregarded by setting the handled property to true.

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    // allows only letters
    if (!char.IsLetter(e.KeyChar))
    {
        e.Handled = true;
    }
}

The second box is restricted to allow only numbers to be entered into the text box.  This works in a manner consistent with the first example except instead of using  "IsLetter" it uses "IsNumber" to evaluate the input.

private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
    // allows only numbers
    if (!char.IsNumber(e.KeyChar))
    {
        e.Handled = true;
    }
}

The third text box in the form restricts the user input to allow only letters or numbers to be entered; special characters are not allowed.  This method uses the character structure's "IsLetterOrDigit" method to evaluate the user's input.

private void textBox4_KeyPress(object sender, KeyPressEventArgs e)
{
    // allows only letters or numbers
    if (!char.IsLetterOrDigit(e.KeyChar))
    {
        e.Handled = true;
    }
}

The last text box control restricts the user to input only special characters; this uses the same "IsLetterOrDigit" call as shown in the previous example, however, instead of looking for a negative match, it looks for a positive match.

private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
{
    // allows only special characters
    if (char.IsLetterOrDigit(e.KeyChar))
    {
        e.Handled = true;
    }
}

Summary

This article demonstrates a few different ways to restrict a user's input at the level of the form.  Aside from the examples shown in this document, one may also consider the use of masked text boxes as a means of restricting user input to a specific string format.


Similar Articles