I have a combo Box In which I made a collection of dates from 1 to 31
and also I have a checklist box in which I made a collection of month
from Jan to Dec.
Now I have to put validation on date that if user select 31 and select month Jan, Feb, Mar then a message popup and
inform them 31 does not exist in February
Given date in format date, month and year in integer. The task is to find whether the date is possible on not. Valid date should range from 1/1/1800 – 31/12/9999 the dates beyond these are invalid. These dates would not only contains range of year but also all the constraints related to a calendar date.
PHP checkdate() Functionvar_dump(checkdate(12,31,-400));
JavaScript Date getMonth() getMonth() returns the month (0 to 11) of a date.
You want the DateTime.DaysInMonth
method:
public bool IsDateValid(int year, int month, int day) {
return day <= DateTime.DaysInMonth(year, month);
}
I'm assuming that the year
and month
values will always be sensible, and that day
will always be greater than or equal to 1. You could easily add argument validation. I'm also assuming that you already have the month as a number, rather than just a name. (Differentiate between the display format of a value and the underlying value.)
Note that you'll need a year picker, otherwise you won't know whether February 29th is valid or not.
(And as Mitch says, it would be better to design a UI where invalid choices simply don't appear, ideally.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With