• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 957
  • Last Modified:

Check weekday

I need to validate that a form field is a particular weekday (say Saturday).  The field is submitted in yyyy-mm-dd format.
0
Orroland
Asked:
Orroland
  • 2
1 Solution
 
nizsmoDeveloperCommented:
Here is a simple example, which may help:


<script type="text/javascript">

// 2007 January 14th as an example
var d=new Date(2007,0,14)

var weekday=new Array(7)
weekday[0]="Sunday"
weekday[1]="Monday"
weekday[2]="Tuesday"
weekday[3]="Wednesday"
weekday[4]="Thursday"
weekday[5]="Friday"
weekday[6]="Saturday"

document.write("Today it is " + weekday[d.getDay()])

</script>

Hope this helps.
0
 
b0lsc0ttCommented:
Orroland,

The function below will take the field value, process it and check for the weekday.

function myDate(val) {
      var arDate = val.split("-");
      if (arDate.length != 3) {
            alert('Invalid date');
            return false;
      }
      var yr = arDate[0];
      var mnth = arDate[1];
      var dy = arDate[2];
      var dte = new Date();
      mnth = mnth - 1;
      dte.setFullYear(yr);
      dte.setMonth(mnth);
      dte.setDate(dy);
      var wkdy = dte.getDay();
      if (wkdy != 6) { // Saturday, days start at 0 for Sun
            alert('The date isn\'t Saturday.');
            return false;
      }
      return true;
}

Let me know how that works.  I can modify or help you use it if needed.  Let me know if you have any questions or need more information.

b0lsc0tt
0
 
b0lsc0ttCommented:
I'm glad that helped.  Thanks for the grade, the points and the fun question.

bol
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now