ASP.Net 验证到达和到达之间的天数出发日期

发布于 2024-11-07 04:57:24 字数 162 浏览 1 评论 0原文

我有一个要求,需要检查两个日期选择器之间输入的天数之间的验证至日期]。我的要求是不能超过100天。

有没有办法可以使用 ASP.NET 提供的验证器。我可以继续为它编写自定义验证器(客户端和服务器端),但想知道使用 CompareValidator 或 RangeValidator 是否可行?

I have a requirement by which need to check validation between number of days entered between two date selectors [From & To Dates]. My requirement is that it should not exceed 100 days.

Is there a way I can do with asp.net provided validators. I can go ahead and write customvalidator for it (both client and server side), but wondering if that is doable using CompareValidator or RangeValidator?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

农村范ル 2024-11-14 04:57:24

尝试使用自定义验证器:

  <asp:CustomValidator ID="valCustmCheckDate" runat="server" ErrorMessage="The date   difference should not be greater than 100 days" ForeColor="Red" ValidationGroup="LoginUserAdd" ClientValidationFunction="CompareStartAndEndDate"></asp:CustomValidator>   

在 javascript 中调用以下函数:

 function CompareStartAndEndDate(sender,args) {
    var txtFromExpiryDate = document.getElementById('<%=txtFromDate.ClientID %>');//dd/mm/yyyy format
    var txtToExpiryDate = document.getElementById('<%=txtToDate.ClientID %>');//dd/mm/yyyy format

     var a = txtFromDate.value.split('/');
     var b = txtToDate.value.split('/');

     var FromDate = new Date(a[2], a[1] - 1, a[0]);
     var ToDate = new Date(b[2], b[1] - 1, b[0]);

      var newFromDate =FromDate.getTime();
      var newToDate=ToDate.getTime();

      var dateDiffInMilliseconds= newToDate-newFromDate;

     var dateDiffInDays=dateDiffInMilliseconds/(1000 * 60 * 60 * 24)       


    if (dateDiffInDays>100 ) {
            args.IsValid = false;
     }
     else {
             args.IsValid = true;
     }

  }

希望这能为您完成...

Try using custom validator:

  <asp:CustomValidator ID="valCustmCheckDate" runat="server" ErrorMessage="The date   difference should not be greater than 100 days" ForeColor="Red" ValidationGroup="LoginUserAdd" ClientValidationFunction="CompareStartAndEndDate"></asp:CustomValidator>   

Call the following function in javascript:

 function CompareStartAndEndDate(sender,args) {
    var txtFromExpiryDate = document.getElementById('<%=txtFromDate.ClientID %>');//dd/mm/yyyy format
    var txtToExpiryDate = document.getElementById('<%=txtToDate.ClientID %>');//dd/mm/yyyy format

     var a = txtFromDate.value.split('/');
     var b = txtToDate.value.split('/');

     var FromDate = new Date(a[2], a[1] - 1, a[0]);
     var ToDate = new Date(b[2], b[1] - 1, b[0]);

      var newFromDate =FromDate.getTime();
      var newToDate=ToDate.getTime();

      var dateDiffInMilliseconds= newToDate-newFromDate;

     var dateDiffInDays=dateDiffInMilliseconds/(1000 * 60 * 60 * 24)       


    if (dateDiffInDays>100 ) {
            args.IsValid = false;
     }
     else {
             args.IsValid = true;
     }

  }

Hope this will do it for you...

海风掠过北极光 2024-11-14 04:57:24

如果您正在寻找类似的答案,下面的函数将完成工作

        function CheckDateRange(start, end, numberOfDays) {

        // Parse the entries
        var startDate = Date.parse(start);
        var endDate = Date.parse(end);
        // Make sure they are valid
        if (isNaN(startDate)) {
            alert("The start date provided is not valid, please enter a valid date.");
            return false;
        }
        if (isNaN(endDate)) {
            alert("The end date provided is not valid, please enter a valid date.");
            return false;
        }
        // Check the date range, 86400000 is the number of milliseconds in one day
        var difference = (endDate - startDate) / (86400000 * numberOfDays);
        if (difference < 0) {
            alert("The start date must come before the end date.");
            return false;
        }
        if (difference >= 1) {
            alert("The range must not exceed 100 days.");
            return false;
        }
        return true;
    }

从类似的 帖子

Below function will do the work if you are looking after similar kinda answer

        function CheckDateRange(start, end, numberOfDays) {

        // Parse the entries
        var startDate = Date.parse(start);
        var endDate = Date.parse(end);
        // Make sure they are valid
        if (isNaN(startDate)) {
            alert("The start date provided is not valid, please enter a valid date.");
            return false;
        }
        if (isNaN(endDate)) {
            alert("The end date provided is not valid, please enter a valid date.");
            return false;
        }
        // Check the date range, 86400000 is the number of milliseconds in one day
        var difference = (endDate - startDate) / (86400000 * numberOfDays);
        if (difference < 0) {
            alert("The start date must come before the end date.");
            return false;
        }
        if (difference >= 1) {
            alert("The range must not exceed 100 days.");
            return false;
        }
        return true;
    }

Got help from somewhat similar post

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文