The following is perhaps more than you need but it validates input and
converts 2 digit year input to the appropriate rolling 50 year window 4
digit year (i.e. if it is 2067 and you type in 01/01/01, that will convert
to 2101 because it is closer than 2001).

To use call the isDateWarn( ) function (for example like this:
isDateWarn(form.date_field, true, true); ).

This will do the trick and much more.  ANd it is Y2K friendly.  Actually, it
works for dates up to 9999.

Dan
--
Daniel Kirkdorffer
NACN IS: 425-580-6225
Sr. Consultant, Syllogistics LLC
Email: [EMAIL PROTECTED]
Web:   http://www.syllogistics.com/


/*
 * Validates integer strings.
 *
 * Parameter: sInteger - a string to validate as an integer
 * Returns:   true (is integer) or false (is not) boolean
 */
function isInteger(sInteger) {

    var isInt = true;
    inputStr = sInteger.toString(); // in case not a string already
    for (var i = 0; i < inputStr.length; i++) {
        var oneChar = inputStr.charAt(i);
        if (oneChar < "0" || oneChar > "9") {
            isInt = false;
            i = inputStr.length; // break out of loop when bad char found
        }
    }
    return isInt;
}

/*
 * Validates fields are not blank.
 *
 * Parameter: field - a field to validate as not blank
 * Returns:   true (is not blank) or false (is blank) boolean
 */
function isNotBlank(field) {

    var valid = true;
    if (field.value.length == 0) {
        valid = false;
        alert("Data Entry Error:\n\nRequired field cannot be blank.");
        field.focus();
        field.select();
    }

    return valid;
}

/*
 * Gives the correct four-digit year when passed the output
 * of the getYear() function, for all dates past the year 1000.
 * Supposedly necessary because some browsers return
 * a year value minus 1900, while others don't.  In practice
 * MSIE 3.02 and NN 4.06  seem to return a two digit year value.
 *
 * Parameter: year - the year passed
 * Returns:   the corresponding four-digit year
 */
function getFullYear(year) {
    return (year < 1000) ? year + 1900 : year;
}

/*
 * Converts 1 or 2 digit years to 4 digit years.
 *
 * Parameter: sYear    - the year string
 * Calls: getFullYear()
 * Returns:   The converted year string
 */
function to4Year(sYear) {

    // Pad 1 digit years with a leading zero
    if (sYear.length == 1)
        sYear = "0" + sYear;

    // Convert 2 digit years to 4 digit years
    if (sYear.length == 2) {

        var current4Year = getFullYear((new Date()).getYear()).toString();
        var currentCentury = parseInt(current4Year.substring(0,2));
        var current2Year = parseInt(current4Year.substring(2,4));
        var switchYear = 0;
        var startCentury = 0;
        var endCentury = 0;
        if (current2Year + 50 > 100) {
            switchYear = Math.abs(current2Year - 50);
            startCentury = currentCentury;
        } else {
            switchYear = current2Year + 50;
            startCentury = currentCentury - 1;
        }
        endCentury = startCentury + 1;

        if (sYear > switchYear)
            sYear = startCentury + sYear;
        else
            sYear = endCentury + sYear;
    }

    return sYear;
}

/*
 * Validates date strings.  This assumes that all dates
 * we would enter are between 1/1/1000 and 12/31/9999.
 * Any other date will fail.
 *
 * Parameter: sDate    - the date string
 * Parameter: required - true if a value must be entered
 * Calls: isInteger() and to4Year()
 * Returns:   true (valid date) or false (not valid) boolean
 */
function isDateString(sDate, required) {

    var valid = true;

    // Optional field should be ignored if blank
    if (required == false && sDate.length == 0)
        return true;

    var Slash1Pos = sDate.indexOf("/",0);
    var Slash2Pos = sDate.indexOf("/",Slash1Pos + 1);
    var mm = sDate.substring(0,Slash1Pos);
    var dd = sDate.substring(Slash1Pos + 1,Slash2Pos);
    var yyyy = sDate.substring(Slash2Pos + 1,sDate.length);

    // Validate date
    if (isInteger(mm) == false || isInteger(dd) == false || isInteger(yyyy)
== false)
        valid = false;

    // Convert 1 or 2 digit years into 4 digit years
    yyyy = to4Year(yyyy);

    if (yyyy.length != 4)
        valid = false;
    else if (mm < 1 || mm > 12)
        valid = false;
    else if (dd < 1 || dd > 31)
        valid = false;
    else if (mm == 2) {
        if (dd > 29)
            valid = false ;
        else if (dd == 29) {
            if (yyyy % 100 == 0 && yyyy % 400 != 0)
                valid = false;
            else if (yyyy % 4 != 0)
                valid = false;
        }
    } else if (mm == 4 || mm == 6 || mm == 9 || mm == 11) {
        if (dd > 30)
            valid = false;
    }

    if (valid == false)
        alert("Data Entry Error:\n\nInvalid date data entered: " + sDate +
"\n\nPlease enter dates in MM/DD/YYYY or M/D/YYYY format.");

    return valid;
}

/*
 * Converts years to 4 digit years and zero pads month and day parts of a
date.
 * Note: The date string must previously have been validated.
 * We assume it is valid here.
 *
 * Parameter: sDate    - the date string
 * Returns:   The converted date string
 */
function toFullDate(sDate) {

    var Slash1Pos = sDate.indexOf("/",0);
    var Slash2Pos = sDate.indexOf("/",Slash1Pos + 1);
    var mm = "0" + sDate.substring(0,Slash1Pos);
    var dd = "0" + sDate.substring(Slash1Pos + 1,Slash2Pos);
    var yyyy = to4Year(sDate.substring(Slash2Pos + 1,sDate.length));

    sDate = mm.substring(mm.length - 2, mm.length) + "/" +
dd.substring(dd.length - 2, dd.length) + "/" + yyyy;

    return sDate;
}

/*
 * Converts a date string in MM/DD/YYYY format into a date
 *
 * Parameter: sDate - the date string in MM/DD/YYYY format to convert to a
date
 * Returns:   the date
 */
function toDate(sDate) {

    var Slash1Pos = sDate.indexOf("/",0);
    var Slash2Pos = sDate.indexOf("/",Slash1Pos + 1);
    var mm = sDate.substring(0,Slash1Pos);
    var dd = sDate.substring(Slash1Pos + 1,Slash2Pos);
    var yyyy = sDate.substring(Slash2Pos + 1,sDate.length);
    var dateObj = new Date(yyyy, mm - 1, dd);

    return dateObj;
}

/*
 * Validates date fields.
 *
 * Parameter: field    - field containing the date string
 * Parameter: required - true if a value must be entered
 * Parameter: warn     - true if warning messages should be displayed
 * Calls: isNotBlank(), toFullDate() and isDateString()
 * Returns:   true (valid date) or false (not valid) boolean
 */
function isDateWarn(field, required, warn) {

    var valid = true;

    // Required fields must be filled in
    if (required == true && isNotBlank(field) == false)
        return false;

    // Validate date
    valid = isDateString(field.value, required);

    if (valid == false) {
        field.focus();
        field.select();

    } else {
        if (field.value.length != 0) {
            // Convert 1 or 2 year dates to 4 year dates
            field.value = toFullDate(field.value);

            // Warn about old dates
            if (warn == true) {

                var sDate = field.value;

                // Convert the entered date string to a date
                var enteredDate = toDate(sDate);

                // *** The following is an example of a date warning calc
and alert
                var tenDaysFromToday = new Date();
                tenDaysFromToday.setTime((new Date()).getTime() + (10 * 24 *
60 * 60 * 1000));

                // We want to make sure the date isn't mis-entered
                // so we compare it with a date days out (out lead time).
                if (enteredDate.getTime() < tenDaysFromToday.getTime()) {
                    if (confirm("Warning:\n\nThe date entered, " + sDate +
", does not allow 10 business days for completion.\nAre you sure about the
date?\n\nSelect OK if your answer is 'Yes',\notherwise select Cancel to
correct the date.") == false) {
                        field.focus();
                        field.select();
                        valid = false;
                    }
                }
            }
        }
    }

    return valid;
}


> ----------
> From:         ACI Team (Chennai)[SMTP:[EMAIL PROTECTED]]
> Reply To:     ACI Team (Chennai)
> Sent:         Monday, November 08, 1999 10:34 PM
> To:   [EMAIL PROTECTED]
> Subject:      Inbuilt Date validation routine in Java script
>
> Hi everybody,
>
> Any of you please tell me an Inbuilt date validation routine in Java
> script
> which uses the mm/dd/yyyy format.
>
> Thanks.
>
> ==========================================================================
> =
> To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
> JSP-INTEREST".
> FAQs on JSP can be found at:
>  http://java.sun.com/products/jsp/faq.html
>  http://www.esperanto.org.nz/jsp/jspfaq.html
>

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
FAQs on JSP can be found at:
 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html

Reply via email to