A lot has happened with Totally Unclutchable since my last blog entry. After weeks of work, I thought that I had created a fairly robust application. How naive I was.
Our resident testing expert, Andy, was let loose on the app and proceeded to break it at every turn. I realised that I hadn’t done enough to control the data being input into the system. Andy entered strings where there should be integers. He entered characters designed to break my SQL queries. He found errors with the sequencing of steps when adding Cars. He was relentless. This was my introduction to validation. I clearly had a lot of work left to do to remove the Garbage data I was allowing to be used in my application.
What is Validation?
Validating data is when an application makes sure the data being input is of the correct type and is useful. There are several types of validation, but I concentrated on a few areas in particular:
- Presence Check
- Range Check
- Format Check
- Type Check
In order to add a new Car to the database, every field must be filled. For example, I needed to ensure that every car had a Daily Hire rate. I used the Validating and Validated events of a textbox to do this. I wrote the following method to check if the textbox wasn’t empty as well as checking if the content was a positive number. The user is unable to move onto the next textbox until an entry is made.
It is often necessary to only accept values in a certain range. For example, ages must be between 18 and 75 to be accepted. This can be done by setting an upper and lower limit or by using comparative operators to make the checks. I have also gained some experience recently with ASP.NET Web Forms. The built-in validation tools make such a check much easier. RangeValidator simply checks if the input is in between two fixed values.
Totally Unclutchable asks users for email addresses, phone numbers and postcodes. I found it quite complicated to create checks that allowed the correct formats. However, once I was introduced to Regular Expressions (Regex) it made the process much more accurate. Regex allows the programmer to decide which characters can be used and the format they can be used in. As an apprentice developer, I found it daunting to have to write my only expressions. Fortunately, Regular Expressions can easily be found online for the most common formats.
As mentioned in Totally Unclutchable – Part 1, I designed the tables in my database to accept certain data types. For example, the Minimum Age for a Car is an integer; the Daily Hire Rate is a decimal. If my program fails to pass in valid data, it will crash. Therefore, checks for data types need to be done before calling to the database. I found that I needed to convert the text from textboxes to integers, decimals or valid dates. As you can see below, the DateRegistered required a timestamp to be appended for SQL to accept it.
Stopping the problem at its source
As I learned more of the properties of each control, I was able to be more sophisticated with my design. For example, in the Add Booking section, a user needs to select the start and end date of their car rental. I limited the dates available by using the MinDate and MaxDate properties of a DateTimePicker. I had prevented incorrect data from being input through improved design. If possible, it’s worth eliminating the need for validation checks.