-1

Can someone help me change the date format in these functions to YYYY/MM/DD and also help me prevent the default form submit if these checks fail? My database is in YYYY/MM/DD format and I am querying by the date, so this is important. Thank you for the help

function ValidateForm(ctrl){
 var stdate = document.getElementById("start");
 var endate = document.getElementById("end");

 //Validate the format of the start date
 if(isValidDate(stdate.value)==false){
  return false;
 }
 //Validate the format of the end date
 if(isValidDate(endate.value)==false){
  return false;
 }
 //Validate end date to find out if it is prior to start date
 if(checkEnteredDates(stdate.value,endate.value)==false){
  return false;
 }

 //Set the values of the hidden variables
 FROMDATE.value= stdate.value;
 TODATE.value= endate.value;

 return true;
}

//--------------------------------------------------------------------------
//This function verifies if the start date is prior to end date.
//--------------------------------------------------------------------------
function checkEnteredDates(stdateval,endateval){
 //seperate the year,month and day for the first date
 var stryear1 = stdateval.substring(6);
 var strmth1  = stdateval.substring(0,2);
 var strday1  = stdateval.substring(5,3);
 var date1    = new Date(stryear1 ,strmth1 ,strday1);

 //seperate the year,month and day for the second date
 var stryear2 = endateval.substring(6);
 var strmth2  = endateval.substring(0,2);
 var strday2  = endateval.substring(5,3);
 var date2    = new Date(stryear2 ,strmth2 ,strday2 );

 var datediffval = (date2 - date1 )/864e5;

 if(datediffval <= 0){
  alert("Start date must be prior to end date");
  return false;
 }
 return true;
}
//--------------------------------------------------------------------------
//This function validates the date for MM/DD/YYYY format. 
//--------------------------------------------------------------------------
function isValidDate(dateStr) {

 // Checks for the following valid date formats:
 // MM/DD/YYYY
 // Also separates date into month, day, and year variables
 var datePat = /^(\d{2,2})(\/)(\d{2,2})\2(\d{4}|\d{4})$/;

 var matchArray = dateStr.match(datePat); // is the format ok?
 if (matchArray == null) {
  alert("Date must be in MM/DD/YYYY 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
}
  • 2
    Weclome to SO. Please do a minimum of investiation youself. Your date check is very inefficient. Use one that creates a date object and compares with the input. http://stackoverflow.com/search?q=%5Bjavascript%5D+validate+date – mplungjan Nov 22 '14 at 06:53

1 Answers1

0

mplungjan gave some links to date validation routines, here's the one I like (maybe I'm biased…):

How to validate a date?

Modifying it for your format:

function isValidDate(s) {
  var bits = s.split(/\D+/);
  var d = new Date(bits[0], --bits[1], bits[2]);
  return d && (d.getMonth() + 1) == bits[1] && d.getDate() == Number(bits[2]) || NaN;
} 

So your form can do something like:

<form onsubmit="return isValidDate(this.start.value) && isValidDate(this.end.value)" ...>

though you might want the validation to be a bit more sophisticated than that.

Edit

So I have a start date and an end date, I need to verify a few things, 1.) that both dates are in format YYYY/MM/DD 2.) that the end date is not prior to the start date and 3.) that they have entered the correct amount of days in each month accounting for leap year days in feb, and if any of those are incorrect, do not submit the form

Well, you should be able to do that from what has been provided. Try:

function validateForm(form) {
  var start = form.start.value;
  var end   = form.end.value;
  var startDate = parseYMD(start);
  var endDate   = parseYMD(end);

  return startDate < endDate && isValidDate(start) && isValidDate(end);
}

using the helper:

function parseYMD(s) {
  var b = s.split(/\D+/);
  return new Date(b[0], --b[1], b[2]);
}

and calling it using:

<form onsubmit="return validateForm(this)" ...> 
Community
  • 1
  • 1
RobG
  • 142,382
  • 31
  • 172
  • 209
  • This seems much easier, I'm new to JS and I don't have an issue understanding that at all, thank you – dav3ydark007 Nov 22 '14 at 07:41
  • Last bit of curiosity, Where in this would I add an alert to this if the check fails? – dav3ydark007 Nov 22 '14 at 07:57
  • No sure why you do not validate all 3 parts of the date - year, month and date? bits[0] could be any number no? – mplungjan Nov 22 '14 at 12:57
  • Yes, no. Only two parts need to be validated, there can be 2 or 3 incorrect, never just one. E.g. `new Date('2013-02-29')` changes month and date, `new Date('2014-12-32')` changes date, month and year, `new Date('2014-13-1')` changes year and month, etc. (ISO strings used for convenience, not recommended generally). Though the CPU cycles saved are insignificant. ;-) Sorry about the misspelling. :-( – RobG Nov 22 '14 at 21:53