1

Hi I am currently stuck on how to carve up my regex date to then test if the date entered is in the past . if it is I would like to alert this.

I know i some how need to splice my regex but im unsure how to do this any help would be much appreciated. below is my script so far its a pretty long regex but it covers everything including leap years but like i said i know need to break it down by either substr or splice.

//start of datefield
var dateformat=/^(?:(?:31\/(?:0[13578]|1[02])|(?:29|30)\/(?:0[13-9]|1[012])|(?:0[1-9]|1\d|2[0-8])\/(?:0[1-9]|1[0-2]))\/[2-9]\d{3}|29\/02\/(?:[2-9]\d(?:0[48]|[2468][048]|[13579][26])|(?:[2468][048]|[3579][26])00))$/;

if (!date.match(dateformat))
{
    alert("format incorrect use dd/mm/yyyy make sure you are entering correct days to the month remember 30 days have september, april, june & november, only 28 days in february unless leap year next is 2016");
    return false;
}
//end date field
Jorge Y. C. Rodriguez
  • 3,394
  • 5
  • 38
  • 61
adam
  • 19
  • 2
  • 9

2 Answers2

1

Simple

date = "12/11/2009";
if(new Date(date) < new Date()){
     // a
} else {
     // b
}
apscience
  • 7,033
  • 11
  • 55
  • 89
  • Note that parsing of non–standard date strings is implementation dependent. The OP wants d/m/y, but most browsers treat strings such as those above as m/d/y (i.e. 11 December 2009). So date strings should be manually parsed to be certain they are handled as required, not as happens by chance. ;-) – RobG Jun 23 '13 at 11:49
  • Hi Rob thanks for the help so do i add what you wrote to the bottom of my script , sorry i am very new to javascript , and thought i could use my regex to validate the correct dat but then somehow take parts from it to then check to see if it is in the past , could you some how send me a working script to 1. check the date is valid including checking for leap years , and then once that is checked to check to see if date entered is date in the past if so show an alert, thank you for taking the time to help. – adam Jun 23 '13 at 17:15
  • @adam—I think my answer does both those. You can combine it in one function if you like, but most prefer utility functions to do one thing only. – RobG Jun 24 '13 at 00:20
  • Hi rob is there anyway I can inbox you a private message to show you my full code as i am sill struggling to understand what you mean, i basically have written up a whole form and want all the validating in one function the thing i am struggling with is the date validating and combining all alerts into one alert box if you could help i would be so greatful as it is really stressing me out now :(. – adam Jun 24 '13 at 07:12
1

Using a regular expression to work out if a date is valid or is before or after some other time is not the easiest way to do the job. Much easier to turn the string into a date object and test that.

So parse the string to create a date object and go from there. You shouldn't leave parsing of date strings to the date object as it is mostly implementation dependent (ECMA-262 specifies a version of ISO8601 but it is not supported by all browsers in use). So if your format is d/m/y you can do:

function isDateHistory(s) {
  s = s.split('/');
  return (new Date(s[2], --s[1], s[0])) < (new Date());
}

alert(isDateHistory('15/6/2013')); // true
alert(isDateHistory('15/7/2013')); // false

You can also validate the date using:

function validateDate(dateString) {
  var s = dateString.split('/');
  var d = new Date(s[2], --s[1], s[0]);
  return d && d.getFullYear() == s[2] && d.getDate() == s[0];
}
RobG
  • 142,382
  • 31
  • 172
  • 209
  • Thank you rob I am awfully sorry but i still dont understand what to actually write below i have attached my entire script it is a complete form the only thing i need to rectify is the date field , please could you talk me through step by step how to insert this into my script correctly your help and time is very much appreciated . – adam Jun 24 '13 at 07:06