Skip to main content

Back To Basics - Validating a date

Recently, a lot of my time has been spent working on systems written by someone else. One of the biggest problems I see in these systems is a lack of basic validation. Limiting the length of strings, requiring values and making sure values fall into the allowed range.

The one that annoys me the most is date validation. Let's be clear, I'm talking about some basic rules to make insure that a date is not just a valid date but is a reasonable value for the data element. January 1, 1900 is a valid date except if we're talking about a birth date for living people. The current oldest living person is 116 years old and there aren't that many people over 100. Ask yourself this question, "Are they going to be in my system?" Probably not, so don't allow it.

This simple rule will keep a lot date typos out of your system. Remember, dates come in different formats MM/dd/yyyy or dd/MM/yyyy or yyyy/MM/dd. Some users will enter zeros such as 08/02/1991 while others might enter 8/2/91. We can't eliminate all typos but at least we can get worst ones.

What other rules can we put in place? Here's the two questions you should be asking about any dates in your system.

  • What is the minimum and maximum range?
  • Is the date related to some other date that we can use to validate?

Let's say we're developing a tracking system for a health screening company. These are the people that make sure you're not already dead or dying when you buy life insurance. They need to capture the applicant's date of birth, date the test was ordered and date the screening was performed. The insurance company only insures people between the date of 21 and 65.
We can come define some simple rules very quickly.

  1. Date of birth must be between the current date minus 65 years and the current date minus 21 years.
  2. The date test was ordered must be between the date of birth and the current date.
  3. The date of screening must be between the date the test was ordered and the current date.

That's all it took. I would probably look to limit the range on the date the test was ordered because it seems like these should be entered in a timely manner. Maybe six months or a year.
So who is responsible for the developing the rules? The project manager? Business analyst? User? The real answer is you, the developer. Why? The short answer is because in the end, it's your code that is going to be blamed.

The real answer is you're the professional. When people hire a professional, they are paying to get a someone who is more knowledgeable about the details. They expect someone who is going to think "you cannot have a date of birth in the future in this system." You know the look you're going to get when people discover you can enter a birth date of 02/22/2233.
So if you're working with a date element take the time to make sure you get the answers for two simple questions.


Popular posts from this blog

Migrating Legacy Apps to the New SimpleMembership Provider

Asp.Net MVC4 uses the new SimpleMembership provider, changes the table structure and adds a new hashing algorithm. The reasons for the changes can be found in this article by Jon Galloway. This article shows how to migrate your existing apps to the new provider.I’m assuming that you stored your passwords in the unrecoverable SHA-1 format. If you didn’t, then you’ll have to change a couple of things. All of my apps are done this way so… I’m also assuming that you have created the basic skeleton of the new app and ran it once so the correct tables will be created.First, we’ll look at the new tables. Previously, we had all of those aspnet_xxxxxx tables. Here’s the new ones.UserProfileContains all of the elements relevant to the user. This is a combination of the aspnet_Users table and the aspnet_Profiles table.webpages_MembershipStores the password info when not using OAuth, Live, Facebook, etc. This table is somewhat of a match to the aspnet_Membership table.webpages_OAuthMembershipStor…

JavaScript function to automatically add slashes to date

In converting an old Windows app to a browser app, the user wanted to be able to enter dates without the slashes. Here's a simple jscript: 1:// Function to convert short date string (MMddyy) 2:// or (MMddyyyy) to a date string (mm/dd/yyyy). 3:// txtBox is the actual textbox control 4:// with the value to be processed. 5:function FixShortDate(txtBox) { 6:if (txtBox == null) { 7:return'' } 8: 9:var re = new RegExp(/(\d{6})(\d{2})?/); 10: 11:if (re.test(txtBox.value)) 12: { 13:if (txtBox.value.length == 8) { 14: txtBox.value = txtBox.value.substring(0, 2) + '/' + txtBox.value.substring(2, 4) + '/' + txtBox.value.substring(4, 8) 15: } 16: 17:if (txtBox.value.length == 6) { 18:if (txtBox.value.substring(4, 6) < 20)