简体   繁体   中英

How could I validate the date range in C# using Jquery

my fromdatepicker1 and todatepicker2. I want to validate the date range of the two. and it will always be fromdate is < todate. and my date format is dd/mm/yy, how should I write it in Jquery?

here is my from date textbox

<asp:RequiredFieldValidator ControlToValidate="txtFromDateBank" 
                        ID="RequiredFieldValidator9" runat="server" Display="Dynamic" 
                        ErrorMessage="asdf" SetFocusOnError="true" InitialValue="" 
                        EnableClientScript="true" Font-Italic="True" Font-Size="Smaller" 
                        ForeColor="Red" ValidationGroup="AddBankDetails">Please specify date</asp:RequiredFieldValidator>
                </div>

heres my todate textbox

<asp:TextBox ID="txtToDateBank" runat="server"  class="datepicker"></asp:TextBox>

use the CustomeValidator control and set the ClientValidationFunction property to a javascript/jquery function. the js function might look like this -

<script language="JavaScript">
<!--
function CheckDate(sender, args)
{
    var strDate = $(fromdatetextbox).val();
    var dateParts = strDate.split("/"); 
    var fromdate = new Date(dateParts[2], (dateParts[1] - 1), dateParts[0]);

    strDate = $(todatetextbox).val();
    dateParts = strDate.split("/"); 
    todate = new Date(dateParts[2], (dateParts[1] - 1), dateParts[0]);

    if(fromdate > todate)
        args.IsValid = false;
    else
        args.IsValid = true;
}
// -->
</script>

you can learn more about customvalidator in this link http://www.4guysfromrolla.com/articles/073102-1.aspx

Update

here is what the full code should look like

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs"   Inherits="Default2" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript">
    <!--
    function CheckDate(sender, args) {
        var strDate = $('#<%= textBoxFromDate.ClientID %>').val();
        var dateParts = strDate.split("/");
        var fromdate = new Date(dateParts[2], (dateParts[1] - 1), dateParts[0]);

        strDate = $('#<%= textBoxToDate.ClientID %>').val();
        dateParts = strDate.split("/");
        todate = new Date(dateParts[2], (dateParts[1] - 1), dateParts[0]);

        if (fromdate > todate)
            args.IsValid = false;
        else
            args.IsValid = true;
    }
    // -->
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:TextBox ID="textBoxFromDate" runat="server" Text=""></asp:TextBox>
            <asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="CustomValidator"
            ControlToValidate="textBoxFromDate" ClientValidationFunction="CheckDate"></asp:CustomValidator>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="RequiredFieldValidator"
            ControlToValidate="textBoxFromDate"></asp:RequiredFieldValidator>


            <asp:TextBox ID="textBoxToDate" runat="server" Text=""></asp:TextBox>
            <asp:CustomValidator ID="CustomValidator2" runat="server" ErrorMessage="CustomValidator"
            ControlToValidate="textBoxToDate" ClientValidationFunction="CheckDate"></asp:CustomValidator>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="RequiredFieldValidator"
            ControlToValidate="textBoxToDate"></asp:RequiredFieldValidator>
        </div>
    </form>
</body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM