Tuesday, May 29, 2012

Date Comparison ASP.NET + JAVA SCRIPT

Download Ajax Control Tool Kit form here
http://ajaxcontroltoolkit.codeplex.com/releases/view/33804

How to Add Ajax Toolkit to SharePoint Web.Config Refer below Link
http://inspiredbytechnology.com/using-ajax-control-toolkit-sharepoint-2010/

<%@ Register Assembly="AjaxControlToolkit, Version=3.0.30930.28736, Culture=neutral, PublicKeyToken=28f01b0e84b6d53e"
    Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>


<script type="text/javascript">
        function ValidateDates(sender, args) {
            var fromDate = document.getElementById('<%= txtMyDate.ClientID %>').value;

            var validformat = /^\d{2}\/\d{2}\/\d{4}$/
            if (!validformat.test(fromDate)) {

                sender.errormessage = "Invalid Date Format. Please correct and submit again. ";
                args.IsValid = false;
                return false;
            }
            else {
                var dayfield = fromDate.split("/")[0]
                var monthfield = fromDate.split("/")[1]
                var yearfield = fromDate.split("/")[2]
                var dayobj = new Date(yearfield, monthfield - 1, dayfield)
                if ((dayobj.getMonth() + 1 != monthfield) || (dayobj.getDate() != dayfield) || (dayobj.getFullYear() != yearfield)) {

                    sender.errormessage = "Invalid Day, Month, or Year range detected. Please correct and submit again. ";
                    args.IsValid = false;
                    return false;
                }
                else {
                    var dtToday = new Date();
                    if (dayobj > dtToday) {
                        args.IsValid = false;
                        return false;
                    }
                    else {
                        args.IsValid = true;
                        return true;
                    }
                }
            }
        };  
    </script>


 <asp:ValidationSummary ID="ValidationSummary1" HeaderText="Please enter valid values for the following field(s)"
                    DisplayMode="BulletList" EnableClientScript="true" runat="server" ValidationGroup="valGroup1"
                    CssClass="error_msg" />



 <asp:TextBox ID="txtMyDate" runat="server" CssClass="myDesign"></asp:TextBox>


<img id="imgMyImage" src="/_layouts/images/MyProject/calander.png" width="20"
                                                height="20" alt="" class="icon" />
                                            <div style="width: 50px; position: relative;">
                                                <ajaxToolkit:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="txtMyDate "
                                                    Format="dd/MM/yyyy" PopupButtonID="imgMyImage" CssClass="cal_Theme1" />
                                            </div>
                                            <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ControlToValidate="txtMyDate"
                                                ErrorMessage="Date of Purchase" Display="None" ValidationGroup="valGroup1" Text="*">
                                            </asp:RequiredFieldValidator>
                                            <asp:CustomValidator ID="CustomDateValidator" runat="server" ErrorMessage="Please ensure that 'Date of Purchase' is lesser than or equal to 'Current Date'."
                                                ClientValidationFunction="ValidateDates" Display="None" ValidationGroup="valGroup1"> </asp:CustomValidator>



<asp:Button ID="btnSave" runat="server" Text="Add" OnClick="btnSave_Click" CssClass="button"
                                                ValidationGroup="valGroup1" />
-----------------------------------------------------------------------------------------------------------


<html>
<head>
<script  language="javascript">
function cal()
{
document.f1.y1.value= mydiff(document.f1.n1.value,document.f1.n2.value,"days");
}
function mydiff(date1,date2,interval) {
var second=1000, minute=second*60, hour=minute*60, day=hour*24, week=day*7;
    date1 = new Date(date1);
    date2 = new Date(date2);
    var timediff = date2 - date1;
        return Math.floor(timediff / day);
}        
</script>
</head>
<body>

<form name="f1">
       enter From Date:<input type="text" name="n1">
   <br>enter To Date:<input type="text" name="n2">
   <br>No Of Days: <input type="text" name="y1">
   <input type="button"  value="Get Days"  name="ab" onclick="cal();">
</form>
</body>
</html>




No comments:

Post a Comment