Javascript将在周六和周日进行验证

时间:2014-03-11 06:29:42

标签: javascript php html

我在php页面中有一个html文本框,通常会将日期作为输入。

我想确保文本框不接受星期六和星期日。

所以我使用以下代码:

if ($filing_date_from != "")
{
    var d = filing_date_from.getDay();
    if (d == '6' || d == '0')
    {
        alert('Dont Enter Sat/Sun');
    }
}

但是这段代码不起作用。我进入星期六/太阳时没有得到警觉。

3 个答案:

答案 0 :(得分:0)

您在代码的以下行中遗漏了$ sign

var d = filing_date_from .getDay();

应该是

var d = $ filing_date_from .getDay();

记住$ filing_date_from应该是日期对象而不是字符串

答案 1 :(得分:0)

<html>
<head>
<script language="javascript">
/*
* This function returns the Day Name
* INPUT :    appDate : Date in mm-dd-yyyy format
*   seperator: the date seperator
*/

function getDayName(appDate, seperator){


               // Name of the days as Array
               var dayNameArr = new Array ("Sunday", "Monday", "Tuesday", "Wednesday", "Thrusday", "Friday", "Saturday");
               var dateArr = appDate.split(seperator); // split the date into array using the date seperator
               var month = eval(dateArr[0]); 
               var day = eval(dateArr[1]);
               var year = eval(dateArr[2]);
              // Calculate the total number of days, with taking care of leapyear 
              var totalDays = day + (2*month) + parseInt(3*(month+1)/5) + year + parseInt(year/4) - parseInt(year/100) + parseInt(year/400) + 2;
              // Mod of the total number of days with 7 gives us the day number
              var dayNo = (totalDays%7);
              // if the resultant mod of 7 is 0 then its Saturday so assign the dayNo to 7
              if(dayNo == 0){
                   dayNo = 7;
              }
            return dayNameArr[dayNo-1]; // return the repective Day Name from the array


}
</script>

</head>
<body>
             <!-- // Date in mm-dd-yyyy format; "-" is the date seperator we can change as per the date seperator format -->


             <input type="button" value="Click Me Date(10-12-2009) seperator(-)" onClick='javascript:alert(getDayName("10-12-2009", "-"));' />
            <input type="button" value="Click Me Date(10.13.2009) seperator(.)" onClick='javascript:alert(getDayName("10.13.2009", "."));' />
           <input type="button" value="Click Me Date(10/14/2009) seperator(/)" onClick='javascript:alert(getDayName("10/14/2009", "/"));' />


</body>
</html>

答案 2 :(得分:0)

以下示例可以帮助您准确地提供您想要的内容

<?php
  $date = '2014-03-08';
?>
<script language="javascript">
  var $filing_date_from = new Date('<?php echo $date;?>');
  if($filing_date_from!="")
  {
    var d=$filing_date_from.getDay();
    if(d=='6' || d=='0')
    {
     alert('Dont Enter Sat/Sun');
    }
  }
</script>