Archive for the ‘Validation’ Category

Date Field Validation

In .NET, it is really easy to check whether a user is entering a date into a form field, although it’s not necessarily obvious how to do so. What you need to do is include a CompareValidator in your web form. The code would look like this:

<asp:TextBox ID="txtBirthDate" runat="server"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" runat="server"
	ErrorMessage="CompareValidator" ControlToValidate="txtBirthDate"
	Operator="DataTypeCheck" Type="Date"></asp:CompareValidator>

The three important properties that you need to set in the CompareValidator are:

  1. ControlToValidate – This is the ID of the Textbox
  2. Operator – Default is “Equal”, need to change to DataTypeCheck
  3. Type – Default is String, set to Date

This will cause the validator to return the error message when an invalid date is entered. It will accept dates such as “4/18/2004”, “9/12/95” and “02/29/2004”, however would cause the error message to be displayed on “14/14/2000”, “02/29/2005”, and “January 1, 2000”. I’d like to see it accept the last one, however, the date should be in the format mm/dd/yyyy, be sure to tell the users to enter this format.

Of course, to be semantically correct, you’d want to give your Textbox a label on the form, and the CompareValidator a more descriptive error message. Also, be sure to enable server side validation, see “Server-Side Validation“.

This tip came from Peter Blum, on dnrTV. Peter has done a ton of work with validation in .NET. His product, Professional Validation and More looks very promising.

Validation Groups

A cool feature of the validation controls in .NET is the property “ValidationGroup”. With this property, you can easily activate different validation controls based on which button is clicked.

For example, suppose you have a content page that has a search box. In addition, this page has a feedback form at the bottom of the page where you can rate “Was this page useful” from 1 to 5. The requirement for this page is that if you click on the search button, you must have entered a search term, and if you click on the rate page button, you must have selected a numerical rating.

To do this, you would add a required field validator for each of these of these fields. However when you test this page, you discover that when you click on the search button, it complains that you didn’t select a page rating, and when you click to page rating button it complains that you didn’t enter a search term. How do you get around this?

Use the ValidationGroup property. In the validation control for the search term text box and the search button, set the property ValidationGroup =”search”, and in the validation control for the page rating entry field and the page rating button set the property ValidationGroup=”pagerating”. This way, when you click the search button, it only runs the validation controls that have ValidationGroup=”search”, and it will not complain that there is not a page rating entered. Likewise with the page rating button.

Server-Side Validation

A couple of weeks ago, I was talking to someone who is doing a project for us. He is fairly new to .NET programming, so when I told him that I wanted both server-side and client-side validation, it scared him a little bit.

He knew how to do client-side validation using the built-in validation controls in ASP.NET. This is fairly easy to do. You put the validation control into your aspx file, and connect it to the control you want to validate. Then ASP.NET will include the required JavaScript for the client to do validation. The problem is that this JavaScript doesn’t run if the client has JavaScript turned off, or if the client doesn’t support JavaScript.

He thought he’d have to write the code to do the server-side validation. He wasn’t aware that you could use .NET’s validation controls on the server-side as well as client-side. All you need to do is add the following code to the beginning of your button handler:

if (!Page.IsValid)
{
    return;
}

This cause the server-side to run through the validation controls. If any fail, then you return to web page, which will now include the error messages from your validation controls. If all the validation controls pass, then it will go on to the next line in the button handler.

It really is this easy to include server-side validation in ASP.NET!