Monday, May 18, 2015

Check/Validate Date Format in JavaScript

Check/Validate Date Format in JavaScript
function checkDateFormate() {

            var date1 = $('#<%= date1.ClientID %>').val();
            var date2 = $('#<%= date2.ClientID %>').val();
            var date3 = $('#<%= date3.ClientID %>').val();
            var date4 = $('#<%= date4.ClientID %>').val();

          var arrayDates = ["date1", date1, "date2", date2, "date3", date3, "date4", date4];

            var var_return = false;
            var validformat = /^\d{2}\/\d{2}\/\d{4}$/
            var errorMsg1 = "";
            var errorMsg2 = "";
            var errorMsg3 = "";
            var dateVal = "";

            for (var i = 1; i < arrayDates.length; i = i + 2) {
                dateVal = arrayDates[i]; 
                if (dateVal != "") {
                    if (dateVal.length > 0) {
                        if (!validformat.test(dateVal)) {
         //alert("Invalid Date Format. Please correct and save/submit again (mm/dd/yyyy).");
                            errorMsg1 += '\n\u2022  ' + arrayDates[i - 1];
                            alert(errorMsg1);
                        }
                        else {
                            var monthfield = dateVal.split("/")[0]
                            var dayfield = dateVal.split("/")[1]
                            var yearfield = dateVal.split("/")[2]

                            var obj_Date = new Date(yearfield, monthfield - 1, dayfield)
                            obj_Date.setHours(0);
                            obj_Date.setMinutes(0);
                            obj_Date.setSeconds(0);

                            var todayDate = new Date();
                            todayDate.setHours(0);
                            todayDate.setMinutes(0);
                            todayDate.setSeconds(0);

                            if ((obj_Date.getMonth() + 1 != monthfield) || (obj_Date.getDate() != dayfield) || (obj_Date.getFullYear() != yearfield)) {
//alert("Invalid Day, Month, or Year range detected. Please correct and submit again (mm/dd/yyyy).");
                                errorMsg2 += '\n\u2022  ' + arrayDates[i - 1];
                                alert(arrayDates[i - 1]);
                            }
                            else {
                                var_return = true;
                                //if (Date.parse(obj_Date) < Date.parse(todayDate)) {
                       //    alert("Selected Date should be greater than or equal to Today.")
                                //}
                                //else {                               
                                //     var_return = true;
                                // }
                            }
                        }
                    }
                }
                //else {
                //    alert("Please enter Date.");
                //}
            }

            if (errorMsg1.length > 0 || errorMsg2.length > 0) {
                if (errorMsg1.length > 0 && errorMsg2.length > 0) {
                    alert('Invalid Date Format. Please correct and save/submit again (mm/dd/yyyy).' + errorMsg1 + '\n\n' + 'Invalid Day, Month, or Year range detected. Please correct and submit again (mm/dd/yyyy).' + errorMsg2);
                }
                else if (errorMsg1.length > 0) {
                    alert('Invalid Date Format. Please correct and save/submit again (mm/dd/yyyy).' + errorMsg1);
                } else if (errorMsg2.length > 0) {
                    alert('Invalid Day, Month, or Year range detected. Please correct and submit again (mm/dd/yyyy).' + errorMsg2);
                }
            }
            //if (errorMsg3.length > 0) {
            //    alert(errorMsg3);
            //}
            return var_return;
        }

No comments:

Post a Comment

Featured Post

Mention a Channel or Team – Power Automate

Mention a Channel or Team – Power Automate graph.microsoft.com/v1.0/teams/{teamId}/channels/{channelId}/messages Channel: ---------- {   ...

Popular posts