Chapter 14.  Validation and Regular Expressions

Table of Contents

14.1. Input Masks
14.1.1. Exercises: Input Masks
14.2. Validators
14.2.1. Exercises: Validators
14.3. Regular Expressions
14.3.1. Regular Expression Syntax
14.3.2. Regular Expressions: Phone Number Recognition
14.3.3. Exercises: Regular Expressions
14.4. Regular Expression Validation
14.5. Subclassing QValidator
14.6. Exercises: Validation and Regular Expressions
14.7. Review Questions

[ fromfile: validation.xml id: validation ]

Abstract

Input data generally needs to be screened, or validated, to make sure that it is appropriate. Numeric data generally needs to be within certain ranges. String data often must have a certain format. Validation of input data is an important issue that can be handled in an object-oriented way by a combination of Qt classes. This chapter discusses some efficient ways of validating input, including the use of regular expressions.

Validators and input masks give the developer fine-grained control over behavior of a QLineEdit, for the purpose of restricting certain kinds of inputs. Before working with them, keep in mind that Qt also offers predefined input widgets and input dialogs for many common value types already (QDateEdit, QTimeEdit, QCalendarWidget, QColorDialog, QSpinBox, QDoubleSpinBox) that permit the user to easily select values, while permitting the developer to restrict the input and narrow the range of valid values. In the case of QDateEdit, the developer can also choose from a variety of locale-aware date formats to use. See Figure 9.9, or run Qt's Standard Dialogs Example to demonstrate some of them.