Go to DotNetFunda.com
 Online : 3092 |  Welcome, Guest!   Login
 
Home > Articles > ASP.NET > Using a CompareValidator to check input is a valid date
  • Nominate yourself for FREE online training by Microsoft MVP on OOPS, ASP.NET, ADO.NET and Sql Server.
    Brought to you by DotNetFunda.Com. You can refer to your friends as well !

  • Now you can recommend your article from any website to be selected as "Article of the Day" on DotNetFunda.Com website. If approved, that article will be featured on our home page.

General Notice: If you found copied contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.

Submit Article | Articles Home | Search Articles |

Using a CompareValidator to check input is a valid date

 Posted on: 9/12/2009 11:46:31 AM by RtpHarry | Views: 1115 | Category: ASP.NET | Level: Beginner | Print Article
In this article rtpHarry examines the CompareValidator and some of the usage scenarios that aren't immediately obvious when you start using it.

Ask all your .NET related questions/clarifications here to get quicker solution.

Introduction

In this article rtpHarry examines the CompareValidator and some of the usage scenarios that aren't immediately obvious when you start using it.

Additional uses of the CompareValidator

The CompareValidator can do more than just compare two controls. You can also compare it against several of the main .net data types such as Date, Integer, Double and Currency.

To do this you would set Operator="DataTypeCheck" and instead of setting the ControlToCompare or ValueToCompare attributes as you normally would you use the Type="Date" (or any of the data types I have listed above).

Here is an example page which illustrates a very simple usage of it:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="CompareValidatorExample._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>CompareValidator Date Validation</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>
CompareValidator Date Validation</h1>
<asp:TextBox ID="TextBox1" ValidationGroup="CompareValidatorDateTest" runat="server"></asp:TextBox>
<asp:Button ID="ButtonSubmit" runat="server" ValidationGroup="CompareValidatorDateTest" Text="Validate Date" /><br />
<asp:CompareValidator ID="CompareValidator1" Display="dynamic" ControlToValidate="TextBox1"
Type="Date" Operator="DataTypeCheck" Text="Please enter a valid date" runat="server"
ValidationGroup="CompareValidatorDateTest" />
</div>
</form>
</body>
</html>
The ValidationGroup attribute

Here is another little side-tip; the ValidationGroup attribute that I have spread throughout this code is a great way to avoid confusion later on when you end up with more than one group of form items in a page. If for example you have a login box in the top corner of your page then the ValidationGroup attribute will let you stop the "username required" validator firing when you click the Submit button on this example.

Advanced Validation

The CompareValidator is one of those controls that provides great basic features but quickly runs out of steam when your websites require a little bit more customisation.

Two things to watch out for when using the CompareValidator in this way is that the Date type will take the format of whatever locale the server is running in. As a UK developer I have sometimes found the server running in an American locale when I uploaded the site which flipped the month and day around.

Another caveat is currency validation - no currency symbols allowed!

To get around these limitations you have the option of using regular expressions via the RegularExpressionValidator or going all-out and developing your own validators by inheriting the base class BaseValidator.

Further Reading

Interesting?   Share and Bookmark this kick it on DotNetKicks.com


Experience:3 year(s)
Home page:http://runtingsproper.blogspot.com/
Member since:Thursday, August 20, 2009
Level:Bronze
Status: [Member]
Biography:
 Latest post(s) from RtpHarry

   ◘ Using a CompareValidator to check input is a valid date posted on 9/12/2009 11:46:31 AM


Submit Article


About Us | The Team | Advertise | Contact Us | Feedback | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
All rights reserved to DotNetFunda.Com. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks.
(Best viewed in IE 6.0+ or Firefox 2.0+ at 1024 * 768 or higher)