0

would need help on the statement below:

its a form submission for date and its not a mandatory field.

how can i submit without any value ? because currently i can't submit the form without value in date field.

Thank you

function ABC_StringIsDateFormatddMMyyyy(str) {
    if (str.length != 8) {
        return false;
    }
    var strYear = str.substring(4, 8);
    var strMonth = str.substring(2, 4);
    var strDay = str.substring(0, 2);
    if (isNaN(strYear)) {
        return false;
    }
    if (isNaN(strMonth)) {
        return false;
    }
    if (isNaN(strDay)) {
        return false;
    }

    var d = new Date();
    d.setFullYear(strYear, parseInt(strMonth) - 1, strDay);
    strYear = d.getFullYear();
    strMonth = d.getMonth() + 1;
    strMonth = "00" + strMonth;
    strMonth = strMonth.substring(strMonth.length - 2, strMonth.length);
    strDay = d.getDate();
    strDay = "00" + strDay;
    strDay = strDay.substring(strDay.length - 2, strDay.length);
    var tmp = strDay + strMonth + strYear;
    if (str != tmp) {
        return false;
    }
    return true;
}
Alson
  • 1
  • 2

1 Answers1

0

Return true if the field is empty.

function ABC_StringIsDateFormatddMMyyyy(str) {
    if (str.length == 0) {
        return true;
    }
    if (str.length != 8) {
        return false;
    }
    var strYear = str.substring(4, 8);
    var strMonth = str.substring(2, 4);
    var strDay = str.substring(0, 2);
    if (isNaN(strYear)) {
        return false;
    }
    if (isNaN(strMonth)) {
        return false;
    }
    if (isNaN(strDay)) {
        return false;
    }

    var d = new Date();
    d.setFullYear(strYear, parseInt(strMonth) - 1, strDay);
    strYear = d.getFullYear();
    strMonth = d.getMonth() + 1;
    strMonth = "00" + strMonth;
    strMonth = strMonth.substring(strMonth.length - 2, strMonth.length);
    strDay = d.getDate();
    strDay = "00" + strDay;
    strDay = strDay.substring(strDay.length - 2, strDay.length);
    var tmp = strDay + strMonth + strYear;
    if (str != tmp) {
        return false;
    }
    return true;
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • facing another issue, how can i prom an error if client put in future date that not valid ? example: today 26 Feb, must put key in value not later then today. mean today 26 feb , you can't key in date after 27 feb. Please help !! and you are awesome – Alson Feb 26 '16 at 05:14
  • http://stackoverflow.com/questions/18712899/check-whether-the-date-entered-by-the-user-is-current-date-or-the-future-date – Barmar Feb 26 '16 at 05:16
  • how can i merge together with the code mention as above ? Thanks – Alson Feb 26 '16 at 07:18
  • The field format doesn't matter, your code above converts it into a `Date` object. So you just have to compare that `d` with `new Date`, which contains the current time and date. – Barmar Feb 26 '16 at 17:59