javascript regex for valid month and day not year

javascript regex for valid month and day not year
cbruneAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Big MontyWeb Ninja at largeCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Big MontyWeb Ninja at largeCommented:
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
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

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

2
0
Big MontyWeb Ninja at largeCommented:
i don't think "2" is a sufficient reason on wanting to delete the question....
0
cbruneAuthor Commented:
good answer
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.