【问题标题】:Simple JavaScript not executing?简单的 JavaScript 没有执行?
【发布时间】:2010-02-08 18:11:44
【问题描述】:

validateDate() 函数在调用时不执行的原因可能是什么?

validateDate() 的目的是取01/01/2001 之类的字符串并调用isValidDate() 来判断日期是否有效。

如果无效,则会出现警告消息。

 function isValidDate(month, day, year){
    /*
    Purpose: return true if the date is valid, false otherwise

    Arguments: day integer representing day of month
    month integer representing month of year
    year integer representing year

    Variables: dteDate - date object

    */
    var dteDate;

    //set up a Date object based on the day, month and year arguments
    //javascript months start at 0 (0-11 instead of 1-12)
    dteDate = new Date(year, month, day);

    /*
    Javascript Dates are a little too forgiving and will change the date to a reasonable guess if it's invalid. We'll use this to our 
    advantage by creating the date object and then comparing it to the details we put it. If the Date object is different, then it must
    have been an invalid date to start with...
    */

    return ((day == dteDate.getDate()) && (month == dteDate.getMonth()) && (year == dteDate.getFullYear()));
 }

 function validateDate(datestring){

     month = substr(datestring, 0, 2);
     day   = substr(datestring, 2, 2);
     year  = substr(datestring, 6, 4);

     if(isValidDate(month, day, year) == false){
         alert("Sorry, " + datestring + " is not a valid date.\nPlease correct this.");
         return false;
     } else {
         return true;
     }
 }

【问题讨论】:

    标签: javascript validation date


    【解决方案1】:

    substr 本身不是一个函数;你必须使用string.substr(start_index, length)

    由于JavaScript substr method 只接受两个参数,而不是三个参数,这会导致执行在第一个 substr 行停止,并且您将永远无法从该函数获得输出。

    我在测试 HTML 页面中运行代码时打开 Firebug 发现了这一点。我强烈推荐使用Firebug 进行 JavaScript 调试。

    在你的 validateDate 函数中试试这个,或者类似的东西:

    month = datestring.substr(0, 2);
    day   = datestring.substr(3, 2);
    year  = datestring.substr(6, 4);
    

    【讨论】:

    • string.substr(start,length) 有效
    • @Upper Stage:根据 w3schools(在我的回答中链接到),“所有主要浏览器都支持 substr() 方法。”我想看看不支持 substr() 的浏览器。
    • 你是对的!它确实需要两个参数。愚蠢的 Dreamweaver 及其代码提示。
    • 我是在回复您的原始、未经编辑的评论(实际上是支持您)。但我很乐意同意;主要,当前浏览器支持 substr.
    • 那么,谢谢!很抱歉混淆了所有的编辑;这就是我滚动的方式。 :)
    【解决方案2】:

    substr 没有定义...你需要

    datestring.substr(0, 2);
    

    您的第二个子字符串也有问题 - 它应该从字符 3 开始:

    day   = substr(datestring, 3, 2);
    

    而且,当您创建日期时,月份确实应该是 (month - 1)

    【讨论】:

      【解决方案3】:

      查看您的代码,您的日期格式是“MMDD__YYYY”。所以你的函数需要如下:

       function isValidDate(month, day, year){
          /*
          Purpose: return true if the date is valid, false otherwise
      
          Arguments: day integer representing day of month
          month integer representing month of year
          year integer representing year
      
          Variables: dteDate - date object
      
          */
          var dteDate;
      
          //set up a Date object based on the day, month and year arguments
          //javascript months start at 0 (0-11 instead of 1-12)
          dteDate = new Date(year, month, day);
          alert(d)
      
          /*
          Javascript Dates are a little too forgiving and will change the date to a reasonable guess if it's invalid. We'll use this to our 
          advantage by creating the date object and then comparing it to the details we put it. If the Date object is different, then it must
          have been an invalid date to start with...
          */
      
          return ((day == dteDate.getDate()) && (month == dteDate.getMonth()) && (year == dteDate.getFullYear()));
       }
      
       function validateDate(datestring){
      
           month = datestring.substring(0, 2);
           day   = datestring.substring(2, 4);
           year  = datestring.substring(6, 10);
           alert(year)
      
           if(isValidDate(month, day, year) == false){
               alert("Sorry, " + datestring + " is not a valid date.\nPlease correct this.");
               return false;
           } else {
               return true;
           }
       }
      validateDate("0202__2010");
      

      如果您的日期格式更常规,您可以执行以下操作来测试if ((new Date("MM/DD/YYYY")) != "Invalid Date")

      【讨论】:

      • 我敢打赌,不是使用像 MMDD__YYYY 这样非常奇怪的东西,substr(2,4) 是一个错字,或者更有可能他们忘记了位置 2 的“/”。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-30
      • 2012-09-15
      • 2017-05-26
      相关资源
      最近更新 更多