Wednesday 15 May 2013

javascript - How to write regex to validate dates? -


I am working in javascript and I need to figure out how to set a valid date using regular expressions To be done

The matches will be:

  DD-MM-Yy dd-mm-yy  

In addition, no leading zero Should be

  9-8-2010 10-6-99  

How can I write a regular expression to do this?

I came up with:

  function isValidDate (inputDate) {Var myRegex = / ^ (\ d {1,2}) ([\ - \ /] (\ d {1,2}) \ 2 (\ d {4} | \ d {2}) $ /; Var match = myRegex.exec (inputDate); If (match! = Null) {var auxDay = match [1]; Var auxMonth = match [3] - 1; Var auxYear = match [4]; AuxYear = auxYear.length & lt; 3? (Oxy's and LT; 70 '20' + Aux Year: '19' + Aux Year): Aux Yure; Var testDate = new date (oaks year, oxmoth, oxide); Return ((auxDay == testDate.getDate ()) & amp; amp; and (auxMonth == testDate.getMonth ()) & amp; amp; amp; auxYear == testDate.getFullYear ())); } And false return; }   

works for dd-mm-yyyy , dd-mm-yy , dm-yyyy

/ html>

No comments:

Post a Comment