asp.net - Using javascript & custom validator to check start date and finish date? - Stack Overflow

I am trying to validate a start date and finish date so that if a finish date entered by a user is befo

I am trying to validate a start date and finish date so that if a finish date entered by a user is before the start date it will throw up an error. I am doing this using JavaScript and the custom validator but get a runtime error saying 'CheckDate is undefined'.

I think this shouldn't be hard to solve as the code looks ok think I'm just missing something.

Any help would be great.

Here is my JavaScript, it is in script tags just haven't copied them over

function CheckDate(sender, args) {
    if (new date (document.getElementById("txtstartdate").value)
        > new (document.getElementById("TxtFinish").value)) {
        args.IsValid = false;
        return;
    }
    args.IsValid = true;
}

Here is the validation on my FinishDate control

<asp:CustomValidator ID="CustomValidator29" runat="server" 
ErrorMessage="Finish Date should be greater than the Start Date" ClientValidationFunction="CheckDate"></asp:CustomValidator>

Need any more info ask away :).

I am trying to validate a start date and finish date so that if a finish date entered by a user is before the start date it will throw up an error. I am doing this using JavaScript and the custom validator but get a runtime error saying 'CheckDate is undefined'.

I think this shouldn't be hard to solve as the code looks ok think I'm just missing something.

Any help would be great.

Here is my JavaScript, it is in script tags just haven't copied them over

function CheckDate(sender, args) {
    if (new date (document.getElementById("txtstartdate").value)
        > new (document.getElementById("TxtFinish").value)) {
        args.IsValid = false;
        return;
    }
    args.IsValid = true;
}

Here is the validation on my FinishDate control

<asp:CustomValidator ID="CustomValidator29" runat="server" 
ErrorMessage="Finish Date should be greater than the Start Date" ClientValidationFunction="CheckDate"></asp:CustomValidator>

Need any more info ask away :).

Share Improve this question edited Dec 8, 2015 at 20:43 DanM7 2,2463 gold badges29 silver badges47 bronze badges asked Sep 3, 2012 at 11:49 madzcodingmadzcoding 941 gold badge4 silver badges12 bronze badges 7
  • also you need to specify the ControlToValidate attribute for CustomValidator – Ram Mourya Commented Sep 3, 2012 at 11:59
  • Which control would I validate? – madzcoding Commented Sep 3, 2012 at 12:04
  • I still get the error 'Microsoft JScript runtime error: 'CheckDate' is undefined' – madzcoding Commented Sep 3, 2012 at 12:13
  • Is your CheckDate function is embedded your page, or added dynamically from codebehind ? – Hassan Commented Sep 3, 2012 at 12:22
  • if (new Date(document.getElementById("txtstartdate").value.getDate()) > new Date(document.getElementById("TxtFinish").value.getDate())) , javascript is case sensitive replace date with Date and add getDate() which returns the Date and ignores the time, may be during runtime the CheckDate is not available . – Ram Mourya Commented Sep 3, 2012 at 12:23
 |  Show 2 more ments

2 Answers 2

Reset to default 5

You can simply use a CompareValidator!

  <asp:CompareValidator ID="CompareValidator1" runat="server" 
    ControlToCompare="txtStartDate" ControlToValidate="txtEndDate" 
    Display="Static" ErrorMessage="'End Date' must not be earlier than 'From Date'"  Text="*"
    Operator="GreaterThanEqual" SetFocusOnError="True" Type="Date" 
    ValidationGroup="SearchGroup">

Here is what I did, and it validates perfectly. Since I wasn't sure exactly what kind of control you were using, I just used a basic text box. I would enter values like "July 21, 1983 01:15:00".

JavaScript:

<script type="text/javascript" >

        function CheckDate(sender, args) {

            var startDate = new Date(document.getElementById("txtStartDate").value);

            var finishDate = new Date(document.getElementById("txtFinishDate").value);

            if (startDate > finishDate) {
                args.IsValid = false;
            }
            else {
                args.IsValid = true;
            }
        }

    </script>

HTML:

<asp:CustomValidator ID="CustomValidator29" runat="server" 
    ErrorMessage="Finish Date should be greater than the Start Date" 
    ClientValidationFunction="CheckDate" ControlToValidate="txtStartDate">
</asp:CustomValidator>

<asp:TextBox id="txtStartDate" runat="server" />
<asp:TextBox id="txtFinishDate" runat="server" />

Here's a table of values and results:

txtStartDate: July 21, 1983 01:15:00
txtEndDate: July 25, 1983 01:15:00
Valid: Yes

txtStartDate: July 25, 1983 01:15:00
txtEndDate: July 21, 1983 01:15:00
Valid: No

txtStartDate: July 21, 1983 01:15:00
txtEndDate: July 21, 1983 06:15:00
Valid: Yes

txtStartDate: July 21, 1983 06:15:00
txtEndDate: July 21, 1983 01:15:00
Valid: No

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742324218a4422422.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信