Thursday, 20 December 2012

Compare Validation for dates of format dd-MMM-yyyy in ASP.NET using C#/VB.NET

Compare Validation for dates of format dd-MMM-yyyy in ASP.NET using C#/VB.NET
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajc" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Compare Validation for dates of format dd-MMM-yyyy in ASP.NET using C#/VB.NET
    </title>
    <style type="text/css">
        .color
        {
            color: Red;
        }
    </style>
    <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
    <script type="text/javascript" language="javascript">
        function ClearDate(e) {

            if (e.keyCode == '8')
                return true;
            else
                return false;
        }

        function DatesValidator_Validate(source, arguments) {
            var sd = $("input[id$='txtStartDate']").val();
            var ed = $("input[id$='txtEndDate']").val();
            var date1 = Date.parse(sd);
            var date2 = Date.parse(ed);
            if (date1 > date2) {
                arguments.IsValid = false;
            }

        }

    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server" />
        StartDate/End Date:<asp:TextBox ID="txtStartDate" runat="server" TabIndex="5" Width="130px"></asp:TextBox>
        &nbsp;<ajc:CalendarExtender TargetControlID="txtStartDate" Format="dd-MMM-yyyy" runat="server"
            Enabled="true" ID="ccal1">
        </ajc:CalendarExtender>
        to&nbsp;
        <asp:TextBox ID="txtEndDate" runat="server" TabIndex="6" Width="130px"></asp:TextBox>
        <ajc:CalendarExtender ID="ccal2" runat="server" Enabled="True" Format="dd-MMM-yyyy"
            TargetControlID="txtEndDate">
        </ajc:CalendarExtender>
        <ajc:TextBoxWatermarkExtender runat="server" ID="TextBoxWatermarkExtender2" TargetControlID="txtStartDate"
            WatermarkText="dd-MMM-yyyy" WatermarkCssClass="watermarked">
        </ajc:TextBoxWatermarkExtender>
        <ajc:TextBoxWatermarkExtender runat="server" ID="TextBoxWatermarkExtender4" TargetControlID="txtEndDate"
            WatermarkText="dd-MMM-yyyy" WatermarkCssClass="watermarked">
        </ajc:TextBoxWatermarkExtender>
        <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txtStartDate"
            CssClass="color " ErrorMessage="Enter a valid End date e.g (dd-MMM-yyyy)." ValidationExpression="^(0?[1-9]|[12][0-9]|3[01])-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-(19|20)\d\d$"
            SetFocusOnError="true" Display="Dynamic"></asp:RegularExpressionValidator>
        <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ControlToValidate="txtEndDate"
            ErrorMessage="Enter a valid End date e.g (dd-MMM-yyyy)." ValidationExpression="^(0?[1-9]|[12][0-9]|3[01])-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-(19|20)\d\d$"
            SetFocusOnError="true" Display="Dynamic" CssClass="color "></asp:RegularExpressionValidator><br />
        <asp:Button ID="btnValidate" runat="server" Text="Validate" />
        <asp:CustomValidator runat="server" ID="datesValidator" OnServerValidate="DatesValidator_Validate"
            ErrorMessage="Enter End Date is greater than Start Date." CssClass="color "></asp:CustomValidator><br />
     
    </div>
    </form>
</body>
</html>


In C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Validation4dd_MMM_yyyy : System.Web.UI.Page
{
    protected void DatesValidator_Validate(object source, ServerValidateEventArgs args)
    {
        if ((txtStartDate.Text != "") && (txtEndDate.Text != ""))
        {
            DateTime startDate = Convert.ToDateTime(txtStartDate.Text);
            DateTime endDate = Convert.ToDateTime(txtEndDate.Text);

            if (endDate <startDate)
            {
                args.IsValid = false;
            }
        }
    }
}

In VB.NET
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

Partial Public Class Validation4dd_MMM_yyyy
    Inherits System.Web.UI.Page
    Protected Sub DatesValidator_Validate(ByVal source As Object, ByVal args As ServerValidateEventArgs)
        If (txtStartDate.Text <> "") AndAlso (txtEndDate.Text <> "") Then
            Dim startDate As DateTime = Convert.ToDateTime(txtStartDate.Text)
            Dim endDate As DateTime = Convert.ToDateTime(txtEndDate.Text)

            If endDate < startDate Then
                args.IsValid = False
            End If
        End If
    End Sub
End Class



output

No comments:

Post a Comment

© 2012-2014 Aspdotnet-Kishore.blogspot.com. All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.