1

I've been assigned the task of validating a date field that gets populated when creating an invoice. It is a text box with three button objects that allow a user to select a date from a calendar, enter today's date, or remove the date entry.

My task is to make sure that users cannot enter a date that is NOT within the current month (double negative...tricky). My task is to make sure that users can only enter dates within the current month. (better?)

I have no idea how to do this. Should I use the asp controls or do this on the back end?

I'm using VB.NET.

I did not post any code yet because I figure the way to do this is pretty standard. If not, I will post whatever is necessary.

Thank you all again.

JettyJetty
  • 71
  • 1
  • 10
  • Have you considered using jQueryUI's `datepicker`? [Here is a datepicker example](http://stackoverflow.com/questions/18130249/datepicker-limit-current-year/18131475#18131475) that will do what you want. – cssyphus Aug 20 '13 at 18:29

2 Answers2

3

You should be able to just get the month from the date entered, then check that against the current month (extracted from Date.Now()), rejecting the date if the months don't match. This code should probably go in the changed event for the date entry box.

This article may also be helpful:

Validate the date format

Community
  • 1
  • 1
  • 1
    Don't forget to match both month **and year as well.** For example, if today is August 20, 2013, then August 21, 2013 is also in the current month, but August 21, 2014 is not. Otherwise, +1. – Geeky Guy Aug 20 '13 at 18:34
1

You are interacting with the user, so probably wish to validate on the front end, AJAX style. However, have you considered using jQueryUI's datepicker?

Here is a datepicker example that will do what you want. Ignore the question title: OP really wanted to limit date selection to within the same month, and that is what this answer does.

Community
  • 1
  • 1
cssyphus
  • 37,875
  • 18
  • 96
  • 111