SIGN UP MEMBER LOGIN:    
ARTICLE

Custom Validation to Validate Dates with Client-Script

Posted by Dorababu M Articles | JQuery May 31, 2011
In this article I am going to explain how we can validate two dates using custom-validator. This article also covers how to use a JQuery calendar in master pages.
Reader Level:
Download Files:
 

In this article I am going to explain how we can validate two dates using custom-validator. This article also covers how to use a JQuery calendar in master pages.

Java-script for validating dates is as follows:

<script type="text/javascript">

function monthDiff(d1, d2) {
        var months;
            var date1 = new Date(d1);
            var date2 = new Date(d2);
            return (date1 - date2) / (1000 * 60 * 60 * 24);
            return months;
       }

        //function getLeapYear
 
        function difference(d1, d2)
        {
            var hiredate, dob; var diff = 18 * 12;
            hiredate = document.getElementById(d1).value;
            dob = document.getElementById(d2).value;
            var Age = monthDiff(hiredate, dob);
            var compareVal = 365 * 18; //getCompareVal(hiredate,dob);
            if (Age >= compareVal) {
                return true;
                //true
            } else {
                return false; //false
            }
        }

function validatehiredate(value, arg) {
                        arg.IsValid = (difference('ctl00_ContentPlaceHolder1_txtHiredate','ctl00_ContentPlaceHolder1_txtDateofBirth'));
                  }
</script>

To load calendar the script is as follows

 <script type="text/javascript">
        $(function () {
            $('#ctl00_ContentPlaceHolder1_txtHiredate').datepick({ showOnFocus: false, showTrigger: '#calImg' });
        });
    </script>

    <script type="text/javascript">
        $(function () {
            $('#ctl00_ContentPlaceHolder1_txtDateofBirth').datepick({ showOnFocus: false, showTrigger: '#Img1' });
        });
    </script>

In master page include the following

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js"></script>

    <script type="text/javascript" src="js/jquery.datepick.js"></script>

    <style type="text/css">
        @import "css/jquery.datepick.css";
    </style>

Sample Images:

Loading Calendar:

Validation1.gif

Validation

Validation2.gif

Default.aspx:

<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true"
    CodeFile="Default.aspx.cs" Inherits="_Default" Title="Untitled Page" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">

    <script type="text/javascript">

function monthDiff(d1, d2) {
        var months;
            var date1 = new Date(d1);
            var date2 = new Date(d2);
            return (date1 - date2) / (1000 * 60 * 60 * 24);
            return months;
       }

        //function getLeapYear

        function difference(d1, d2)
        {
            var hiredate, dob; var diff = 18 * 12;
            hiredate = document.getElementById(d1).value;
            dob = document.getElementById(d2).value;
            var Age = monthDiff(hiredate, dob);
            var compareVal = 365 * 18; //getCompareVal(hiredate,dob);

            if (Age >= compareVal) {
                return true;
                //true
            } else {
                return false; //false
            }
        }

function validatehiredate(value, arg) {
                        arg.IsValid = (difference('ctl00_ContentPlaceHolder1_txtHiredate','ctl00_ContentPlaceHolder1_txtDateofBirth'));
                  }
    </script>

    <script type="text/javascript">
        $(function () {
            $('#ctl00_ContentPlaceHolder1_txtHiredate').datepick({ showOnFocus: false, showTrigger: '#calImg' });
        });
    </script>

    <script type="text/javascript">
        $(function () {
            $('#ctl00_ContentPlaceHolder1_txtDateofBirth').datepick({ showOnFocus: false, showTrigger: '#Img1' });

        });
    </script>

    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" ValidationGroup="personal"
        Style="left: 459px; position: absolute; top: 171px" />
                <asp:Label ID="lblDate" runat="Server" Text="HireDate" style="left: 321px; position: absolute; top: 98px"></asp:Label>

    <asp:Label style="LEFT: 310px; POSITION: absolute; TOP: 133px" id="Label1" runat="Server" Text="DateofBirth"></asp:Label>
                <asp:TextBox ID="txtHiredate" runat="server" ValidationGroup="personal" Style="left: 397px;
                    position: absolute; top: 96px" />   

                <div style="display: none;">
                    <img id="calImg" src="images/calendar.gif" alt="Popup" class="trigger" style="left: 564px;
                        position: absolute; top: 101px" />
                </div>

    <asp:TextBox ID="txtDateofBirth" runat="server" ValidationGroup="personal" Style="left: 398px;
        position: absolute; top: 131px" />
    <asp:CustomValidator ID="CustomValidator1" runat="server" ClientValidationFunction="validatehiredate"
        ControlToValidate="txtDateofBirth" ValidationGroup="personal" Display="Dynamic"
        Style="left: 596px; position: absolute; top: 132px">can not hire guy less than 18 yrs</asp:CustomValidator>
    <div style="display: none;">
        <img id="Img1" src="images/calendar.gif" alt="Popup" class="trigger" style="left: 568px;
            position: absolute; top: 136px" />
        &nbsp;</div>
</asp:Content>
 

Login to add your contents and source code to this article
share this article :
post comment
 

Thanks nice article.i ve doubt in this, if i want to use the calender in textbox instead of the calender icon mean what should i do?Actually am trying some method but didnt work .I cal the textbox id instead of the image id , but it shows double textbox .

Posted by priya mathiyalagan Mar 02, 2012

Thanks nice article.i ve doubt in this, if i want to use the calender in textbox instead of the calender icon mean what should i do?Actually am trying some method but didnt work .I cal the textbox id instead of the image id , but it shows double textbox .

Posted by priya mathiyalagan Mar 02, 2012
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor