14.6.  Exercises: Validation and Regular Expressions

[ fromfile: ex-validation.xml id: ex-validation ]

  1. Design an Address Form like that shown in Figure 14.6.

    Figure 14.6.  Address Form

    Address Form

    When the user selects a country from the combo box, the program must set an appropriate validator on the stateEdit, zipEdit, and phoneEdit QLineEdit widgets. Also, the dialog labels should change in the following ways:

    1. USA:

      • Set the zipLabel to Zip.

      • Set the stateLabel to State.

    2. Canada:

      • Set the zipLabel to Postal Code.

      • Set the stateLabel to Province.

    3. Add another country of your choice with appropriate validators.

    Make sure that invalid inputs are never accepted (disable the Ok button too). When the user clicks Ok, close the dialog and print out the value of each field. You can start with src/handouts/validation/addressform.ui if you like.

  2. International Standard Book Number (ISBN) is a coding system for books and other publications. ISBNs come in different sizes (10-digit and 13-digit) and they break down into substrings that identify language group, publisher, and title. In addition, the rightmost digit is a check digit that is computed from the other digits for error detection. [92]

    Assume that the ISBN-10 code (including the check digit) looks like this:

    d1d2d3...d9d10

    Then the following equation must balance:

    (10*d1 + 9*d2 + 8*d3 + ... + 2*d9 + d10) % 11 = 0

    If the required value for the check digit is 10, use the letter 'X' in that position.

    Define a QValidator that accepts an ISBN-10 code if and only if it has a correct check digit. You can test it with 0-306-40615-2 (valid) and 0-306-40615-5 (invalid).

  3. Find out (e.g., from the Wikipedia article) how to validate the check digit for an ISBN-13 code and define a QValidator that accepts one if and only if its check digit is correct.

  4. Can you make a single QValidator that can handle both ISBN-10 and ISBN-13 codes (validating only the check digit)?



[92] There is an interesting article in Wikipedia that explains the system.