【问题标题】:Why this function load the server?为什么这个函数加载服务器?
【发布时间】:2017-05-19 07:20:06
【问题描述】:

我有这个功能。

这是一个注册页面的验证,需要检查日期是否有效。

我有另一个运行良好的函数,如果验证失败则不会运行。

我认为它可能在验证器本身,但正如我所说,我有另一个运行良好的函数(以及另一个调用另一个函数的验证器),但我没有找到任何差异。

代码:

失败的功能:

 function CheckDate(sender, args)
      {

          var DateReg = document.getElementById("DateReg").value;
          DateReg = DateReg.split('/');

          var day = DateReg[0];
          var month = DateReg[1];
          var year = DateReg[2];


          var CurrentYear = new Date().getFullYear();

          args.IsValid = true;  

         if (day < 1 || day > 31 || month < 1 || month > 12 || year > CurrentYear || year < CurrentYear - 120)
          {
             args.IsValid = false; // לא תקין
         }

          return args.IsValid;
      }

验证器失败:

        <asp:CustomValidator ID="CustomValidator1" ClientValidationFunction="CheckDate" runat="server" ErrorMessage="type age again">&nbsp;</asp:CustomValidator>

工作验证者:

         <asp:CustomValidator ID="CustomValidator2" ClientValidationFunction="CheckInterest" runat="server" ErrorMessage="choose interest">&nbsp;</asp:CustomValidator>

有什么建议吗?

谢谢

【问题讨论】:

  • 您正在读取一个不存在的数组date,并且您没有在任何地方设置day
  • @ChrisG 已编辑,查找
  • 您的有效日期不会捕获无效日期。 “2/31/2018”将通过您的支票有效......
  • 控制台有错误吗?

标签: javascript asp.net validation


【解决方案1】:

好的;明白了

getElementById 做到了

改成

var DateReg = document.getElementById('<%=DateReg.ClientID %>').value;

现在好像可以了

【讨论】:

    【解决方案2】:

    将在以下格式“mm/dd/yyyy”的日期验证的修改版本。

    var btnRun = document.getElementById("btnRun");
    
    btnRun.addEventListener('click',function(e){
      CheckDate();
    });
    
    function CheckDate(sender, args)
         {
              var isValid = false;
              var DateReg = document.getElementById("DateReg").value;
              var date = new Date(DateReg);
              var cDate = new Date();
              var CurrentYear = cDate.getFullYear();
              isValid = true;
              var iday = date.getDay() + 1;
              var iMonth = date.getMonth() + 1;
              var iYear = date.getFullYear();
    
             if (iday < 1 || iday > 31 || iMonth < 1 || iMonth > 12 || iYear > CurrentYear || iYear < CurrentYear - 120)
              {
                  isValid = false;
              }
              console.log(isValid);//just used to show that it works
              return isValid;
          }
    <input id="DateReg" />
    <button id="btnRun">Run</button>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-02
      • 2013-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-09
      • 2016-05-16
      相关资源
      最近更新 更多