2

i need a javascript method to validate a date pattern looks like this Jan 14, 2012

so far i am not sure which regex to use for this pattern as it is not standard date such as 14/01/2012 or 01/14/2012 or 01-04-2012..i tried to do something like this for early startes to split them up but ending i received a total split.

here is my split code

 function validateDate(date){
   var test = date.replace(",","");
   alert(test);
  var split = test.split("");
  for(i=0;i<split.length;i++){
      alert(split[i]);
   }
  alert("total length "+split.length);
}
user1897151
  • 493
  • 2
  • 9
  • 28
  • Can you give more input examples? – Justin McDonald Dec 12 '12 at 08:41
  • i have a date input format which looks like this Jan 14, 2012 so i would like to validate this field to make sure that user are adding those date in a correct format. but this kind of date are not standard so is hard to do validation i guess – user1897151 Dec 12 '12 at 08:44

5 Answers5

3

You can use momentjs and particullary its parsing functionnality. Something like :

moment("Jan 14, 2012", "MMM DD YYYY").isValid();
Alexandre Ardhuin
  • 71,959
  • 15
  • 151
  • 132
0

Use the regex:

^[a-zA-Z]{3}\s[0-9]{1,2},\s[0-9]{4}$

This will work for any date with month of three char, with comma after day, and 4 digit year, in that order

Justin McDonald
  • 2,156
  • 16
  • 19
0

You can use something like this:

(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec), [0-3]?\d (\d\d)?\d\d

Please fill free to play with this expression to tune it up

Mikhail Payson
  • 923
  • 8
  • 12
0

You could convert that format to the format you want. All you need to do is extract the parts:

function formatDate( dateStr ) {
  var date = new Date( dateStr ),
      m = date.getMonth() + 1,
      d = date.getDate(),
      y = date.getFullYear();
  return m +'/'+ d +'/'+ y; // you can use any format here
}

console.log( formatDate( 'Jan 14, 2012' ) ); //=> '1/14/2012'

Then you can use the new string with this function: Best way to validate date string format via jQuery. That will effectively validate a date so "Feb 29, 2011" won't validate for example as mplungjan points out.

Usage:

if ( isValidDate( formatDate( 'Jan 14, 2012' ) ) ) {
  // valid
} else {
  // not valid
}
Community
  • 1
  • 1
elclanrs
  • 92,861
  • 21
  • 134
  • 171
  • Feb 29, 2011 is valid in your case – mplungjan Dec 12 '12 at 09:05
  • It won't validate, follow [the link](http://stackoverflow.com/questions/11218181/best-way-to-validate-date-string-format-via-jquery/11218271#11218271) for date validation. It'll work correctly once you pass the right format. Demo here http://jsfiddle.net/elclanrs/jeJxr/ – elclanrs Dec 12 '12 at 09:07
0

It is a valid string to pass to the date object

DEMO

var monthNames = "Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec".split(",");
function isvalid(str) {
  var d = new Date(str);
  var dateParts = str.split(" ");
  return monthNames[d.getMonth()]===dateParts[0] &&
         d.getDate()===parseInt(dateParts[1],10) &&
         d.getFullYear()===parseInt(dateParts[2],10);
}      
alert(isvalid("Jan 14, 2012"));
alert(isvalid("Feb 29, 2011"));

mplungjan
  • 169,008
  • 28
  • 173
  • 236