Date validation - Dropdownlist

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

   I have to do some date validation in 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 (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 142

    Expert Comment

    by:Guy Hengel [angelIII / a3]
    LVL 1

    Accepted Solution

    Here is a good start....modify the following to create a string out of the values of your three controls, and check for validity.

    <SCRIPT LANGUAGE="JavaScript">
    <!-- Original:  Sandeep V. Tamhankar ( -->

    <!-- This script and many more are available free online at -->
    <!-- The JavaScript Source!! -->

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


       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.



    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    760 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

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now