0

How do you check if the two dates are in a same month and year? And which validation control should I use to have this? I'm thinking of using client-side validation (if it's possible)

Thanks!

Musikero31
  • 3,115
  • 6
  • 39
  • 60

3 Answers3

2

You could use a CustomValidator

aspx:

<asp:CustomValidator runat="server"
    ID="valMonth" 
    onservervalidate="ServerValidation" 
    onclientvalidate="ClientValidate"
    ErrorMessage="From and To must be in the same month" />

servervalidate:

protected void ServerValidation (object source, ServerValidateEventArgs args)
{     
    DateTime dtFrom;
    DateTime dtTo;
    if(DateTime.TryParse(TxtFrom.Text, out dtFrom) && DateTime.TryParse(TxtTo.Text, out dtTo)) 
    {
       args.IsValid = dtFrom.Year == dtTo.Year && dtFrom.Month == dtTo.Month;
    }
    else
    {
        args.IsValid = false;
    }
}

You should also add two CompareValidators which check if both are valid dates. To provide a ClientValidationFunction you can have a look at the getMonth and getFullYear functions.

For example(not tested):

<script language="javascript">
   <!--
   function ClientValidate(source, arguments)
   {
      var txtFrom = document.getElementById('<%= TxtFrom.ClientID %>');
      var txtTo = document.getElementById('<%= txtTo.ClientID %>');
      var dtFrom = new Date(txtFrom.value);  
      var dtTo = new Date(txtTo.value);  
      var monthFrom = dtFrom.getMonth();
      var monthTo = dtTo.getMonth();
      var yearFrom = dtFrom.getFullYear();
      var yearTo = dtTo.getFullYear();

      arguments.IsValid = yearFrom == yearTo && monthFrom == monthTo;
   }
   // -->
</script>
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
1

Assuming the date is in "dd/MM/yyyy" format, you could use this javascript function with a custom validator.

aspx

<asp:CustomValidator ID="CustomValidator1" runat="server" 
    EnableClientScript="true"
    ErrorMessage="ERROR MESSAGE HERE"
    ClientValidationFunction="checkDate">
</asp:CustomValidator>

javascript

function checkDate() {
   var date1=document.getElementById('<%=txtDate1.ClientID%>').value;
   var date2=document.getElementById('<%=txtDate1.ClientID%>').value;
   var date1Values=date1.split("/");
   var date2Values=date2.split("/");
   if (date1Values[2] == date2Values[2] && date1Values[1] ==date2Values[1]) {
     args.IsValid = true;
   }
   else
   {
     args.IsValid = false;
   }
}

You should have server validate code too if javascript is disabled. See Tim's answer.

Jack Pettinger
  • 2,715
  • 1
  • 23
  • 37
0

Here's a short and simple example using a CustomValidator control

    Date 1<asp:TextBox ID="txtDate1" runat="server" />
    <br />
    Date 2<asp:TextBox ID="txtDate2" runat="server" />
    <br />
    <asp:Button ID="btnCompare" runat="server" Text="Compare" />
    <asp:CustomValidator ID="dateValidator" runat="server" ErrorMessage="The two dates must be in the same month and year"
        OnServerValidate="ValidateDate" />
    <script runat="server">
        protected void ValidateDate(object source, ServerValidateEventArgs args)
        {
            DateTime date1 = DateTime.Parse(txtDate1.Text);
            DateTime date2 = DateTime.Parse(txtDate2.Text);

            if (date1.Month != date2.Month || date1.Year != date2.Year)
                args.IsValid = false;
        }
    </script>
Denys Wessels
  • 16,829
  • 14
  • 80
  • 120