Blog Archive

Javascript Validate a Date String in HTML form

The following Javascript function worked for me, so wanted to share with anyone who is looking for the same. Here is a demo for you to Test and the sample code.

Enter Date: (in MM/DD/YYYY format)

<html>
<head>
</head>
<body>

<SCRIPT LANGUAGE="JavaScript1.2"> <!--

function myDateValidation() {

    var lre = /^\s*/;
    var datemsg = "";
   
    var inputDate = document.as400samplecode.myDate.value;
    inputDate = inputDate.replace(lre, "");
    document.as400samplecode.myDate.value = inputDate;
    datemsg = isValidDate(inputDate);
        if (datemsg != "") {
            alert(datemsg);
            return;
        }
        else {
            //Process your form
        }

}

function isValidDate(dateStr) {

   
    var msg = "";
    // Checks for the following valid date formats:
    // MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY
    // Also separates date into month, day, and year variables

    // To require a 2 & 4 digit year entry, use this line instead:
    //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) {
        msg = "Date is not in a valid format.";
        return msg;
    }

    month = matchArray[1]; // parse date into variables
    day = matchArray[3];
    year = matchArray[4];

   
    if (month < 1 || month > 12) { // check month range
        msg = "Month must be between 1 and 12.";
        return msg;
    }

    if (day < 1 || day > 31) {
        msg = "Day must be between 1 and 31.";
        return msg;
    }

    if ((month==4 || month==6 || month==9 || month==11) && day==31) {
        msg = "Month "+month+" doesn't have 31 days!";
        return msg;
    }

    if (month == 2) { // check for february 29th
    var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
    if (day>29 || (day==29 && !isleap)) {
        msg = "February " + year + " doesn't have " + day + " days!";
        return msg;
    }
    }

    if (day.charAt(0) == '0') day= day.charAt(1);
   
    //Incase you need the value in CCYYMMDD format in your server program
    //msg = (parseInt(year,10) * 10000) + (parseInt(month,10) * 100) + parseInt(day,10);
   
    return msg;  // date is valid
}

// --> </SCRIPT>

<form name="as400samplecode">
Enter Date:  <input type="text" name="myDate" size=10 maxlength=10> (in MM/DD/YYYY format)
<input type="button" value="validate Date" onclick="Javascript:myDateValidation()">
</form>

</body>

</html>

6 comments :

  1. what does this do?

    as400samplecode.myDate.value

    thx

    ReplyDelete
  2. Actually, I'm trying to modify the code to accept dates in the format of

    yyyy/mm/dd

    I should have said that :-)

    thx

    ReplyDelete
  3. as400samplecode.myDate.value is getting the value of the input from the screen. for yyyy/mm/dd you must change these lines

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

    month = matchArray[1];
    day = matchArray[3];
    year = matchArray[4];

    basically move the year pattern to the front which is(\d{4})

    and then year will be index=1, month=3 and day=4

    Give it a try!

    ReplyDelete
  4. Hi,

    I've test this for dd/mm/yyyy :

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

    month = matchArray[3];
    day = matchArray[1];
    year = matchArray[4];

    Without success, if you have an Idea ?

    ReplyDelete