Date validation - Dropdownlist

Posted on 2006-04-16
Medium Priority
Last Modified: 2006-11-18

   I have to do some date validation in asp.net(vb code behind). My situation is, I am using three list boxes, one for date, one for month & one for Year. I want to validate, whether the user selected the correct date format like not 31st for Nov etc & leapyear validation for Feb. How could I do this with asp.net (javascritp)? Do any one having the code for this? At present I have some function in onselectedindexchange event of the year selection. So, I have to combine these two.

Question by:ceema
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 16463146

Accepted Solution

gr33nlant3rn earned 1000 total points
ID: 16466798
Here is a good start....modify the following to create a string out of the values of your three controls, and check for validity.

<!-- Original:  Sandeep V. Tamhankar (stamhankar@hotmail.com) -->

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- Begin
function isValidDate(dateStr) {
// Checks for the following valid date formats:
// Also separates date into month, day, and year variables

var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;

// To require a 4 digit year entry, use this line instead:
// var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;

var matchArray = dateStr.match(datePat); // is the format ok?
if (matchArray == null) {
alert("Date is not in a valid format.")
return false;
month = matchArray[1]; // parse date into variables
day = matchArray[3];
year = matchArray[4];
if (month < 1 || month > 12) { // check month range
alert("Month must be between 1 and 12.");
return false;
if (day < 1 || day > 31) {
alert("Day must be between 1 and 31.");
return false;
if ((month==4 || month==6 || month==9 || month==11) && day==31) {
alert("Month "+month+" doesn't have 31 days!")
return false
if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day>29 || (day==29 && !isleap)) {
alert("February " + year + " doesn't have " + day + " days!");
return false;
return true;  // date is valid
//  End -->


Author Comment

ID: 16476932

   Thank you gr33nlant3rn , sorry for being late in giving points, today only I got time to check it.
Thank you angelIII for the reply, My client need it in this format, so I can't adopt anything else.

Thank you so much for helping me.



Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month13 days, 9 hours left to enroll

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question