Java Script to Validate Saturday and Sunday

I have a html textbox in php page which will usually get date as Input.

I want to make sure that textbox won't accept the Saturday and Sunday.

So I use the following code:

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

But this code is not working. I didn't get alert when I enter the Sat/Sun.

Answers


You have missing $ sign from following line of your code

var d=filing_date_from.getDay();

should be

var d=$filing_date_from.getDay();

Remember $filing_date_from should be date object not the string


<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>

May following example help you exactly what you want

<?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>

Need Your Help

IntelliJ 15 and Grails 3: unable to find grails packages

grails intellij-idea dependency-management

I'm converting a grails 2 plugin to grails 3, using IntelliJ 15.0 Community Edition. For Java files only, IntelliJ gives me many errors about missing packages and symbols from Grails. For example:

rails cancan custom actions

ruby-on-rails ruby ruby-on-rails-3 cancan railscasts

I'm trying to prevent editors of various items from being able to publish their own works using cancan but it doesn't work as expected. Everything else so far works perfectly.