• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 167
  • Last Modified:

javascript regex for valid month and day not year

javascript regex for valid month and day not year
0
cbrune
Asked:
cbrune
  • 3
  • 2
1 Solution
 
Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
i don't recommend using a regular expression for date validation, as it's hard to cover the different number of days for each month. instead, here's a simple date validation function:

function isDate(txtDate)
{
  var currVal = txtDate;
  if(currVal == '')
    return false;

  //Declare Regex 
  var rxDatePattern = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
  var dtArray = currVal.match(rxDatePattern); // is format OK?

  if (dtArray == null)
     return false;

  //Checks for mm/dd/yyyy format.
  dtMonth = dtArray[1];
  dtDay= dtArray[3];
  dtYear = dtArray[5];

  if (dtMonth < 1 || dtMonth > 12)
      return false;
  else if (dtDay < 1 || dtDay> 31)
      return false;
  else if ((dtMonth==4 || dtMonth==6 || dtMonth==9 || dtMonth==11) && dtDay ==31)
      return false;
  else if (dtMonth == 2)
  {
     var isleap = (dtYear % 4 == 0 && (dtYear % 100 != 0 || dtYear % 400 == 0));
     if (dtDay> 29 || (dtDay ==29 && !isleap))
          return false;
  }
  return true;
}

Open in new window


to only validate the month and day, simply append a "dummy" year to the value you pass into the function, that way it'll pass the first initial date format validation

keep in mind this checks for the mm/dd/yyyy format. If you want to validate against the dd/mm/yyyy format, a slight change is needed:

//Checks for mm/dd/yyyy format.
    dtMonth = dtArray[1];
    dtDay= dtArray[3];
    dtYear = dtArray[5];   

Open in new window

0
 
Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
if you have more complex date functionality needs, you may also want to check out DateJS, a jquery date validation plug-in.

https://code.google.com/p/datejs/wiki/APIDocumentation
0
 
Michel PlungjanIT ExpertCommented:
Here is an even simpler test assuming current year.

FIDDLE

function testDate(dd,mm,yyyy) {
  yyyy = yyyy||new Date().getFullYear(); // optional year
  mm = mm-1; // JS Months are 0 based
  var testDate = new Date(yyyy,mm,dd,12,0,0,0);
    window.console&&console.log(testDate); // remove when happy
  return dd == testDate.getDate() && mm == testDate.getMonth() && testDate.getFullYear() == yyyy;
}

var dd = 29; // your date
var mm = 2; // your month
window.console&&console.log(testDate(dd,mm));

dd = 32; // your date
mm = 13; // your month
window.console&&console.log(testDate(dd,mm));

dd = 2; // your date
mm = 3; // your month
window.console&&console.log(testDate(dd,mm));

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
cbruneAuthor Commented:
I've requested that this question be deleted for the following reason:

2
0
 
Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
i don't think "2" is a sufficient reason on wanting to delete the question....
0
 
cbruneAuthor Commented:
good answer
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now