Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regexp to validate a Date (warning, stretched post) 2

Status
Not open for further replies.

kaht

Programmer
Aug 18, 2003
4,156
US
Enjoy:
Code:
<html>
<script language=javascript>
function validateForm()
{var txt = document.forms['blahForm'].elements['blahText'];
if (/(((0[1-9]|[12][0-9]|3[01])([/])(0[13578]|10|12)([/])(\d{4}))|(([0]
[1-9]|[12][0-9]|30)([/])(0[469]|11)([/])(\d{4}))|((0[1-9]|1[0-9]|2[0-8])
([/])(02)([/])(\d{4}))|((29)(\.|-|\/)(02)([/])([02468][048]00))|((29)([/])
(02)([/])([13579][26]00))|((29)([/])(02)([/])([0-9][0-9][0][48]))|((29)
([/])(02)([/])([0-9][0-9][2468][048]))|((29)([/])(02)([/])
([0-9][0-9][13579][26])))/.test(txt.value)) {
      alert('Date validated successfully');
      return true;
   }
   else {
      alert('Your date is invalid');
      txt.focus();
      return false;
   }
}
</script>
<form name=blahForm onsubmit='return validateForm()'>
Enter a date in the format DD/MM/YYYY<br>
<input type=text name=blahText><br>
<input type=submit value='submit form'>
</form>
</html>

-kaht

Do the chickens have large talons?
[banghead]
 
It is for DD/MM/YYYY. CliveC, you'd mis-read other's posting! It is DD/MM/YYYY all along since ca8msm's quote.
- tsuji
 
That's because there's only 12 months in a year, not 29. If you'd pay a bit closer attention you'd notice that it's dd/mm/yyyy.

That's days/months/years in case you don't understand the syntax.

-kaht

Do the chickens have large talons?
[banghead]
 
Unlike many forums on the internet, here at Tek-Tips, Literacy is REQUIRED. Thank you.

-Rick

PS: I am not employed by nor associated with Tek-Tips, the things that escape my mouth/fingers in no way represent the views of Tek-Tips, it's employees, or it's management.

----------------------
[banghead]If you're about to post an ASP.Net question,
please don't do it in the VB.Net forum[banghead]

[monkey] I believe in killer coding ninja monkeys.[monkey]
 
As requested here is another example, using kaht's original html/javascript but with a new reg ex for the format dd/mm/yyyy:


--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
While I was napping my nurse tried a few dates. She was very impressed with the algorithmn because it did not, as we suspected, simply consider every 4th year as a leap year. It also correctly noted that 01/01/10000 was valid.

She was able to find a problem with this date however:
03/09/1752 which is not a valid date.

Under the circumstances and since 1752 was so long ago, I vote that we all give a star to Kaht for having the gumption to provide a fully tested working example.

Congrats Kaht... You are a star. Here is mine!

Clive
 
is this for upper-case or lower-case numbers?

-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
i like your sleeves...they're real big
 
Date problems in 1752 ...

Below are the first 2 paragraphs from this page:

-------------------------------
One of the problems in doing Early American research is the 1752 date change. Prior to 1752, all of England and her colonies were using the Julian calendar to report ecclesiastical, legal, and civil events. In 1752, they all changed to the Gregorian calendar. In order to properly interpret dates prior to 1752, one must understand the difference between the Julian and Gregorian calendars.

On the Julian calendar the first day of the year was March 25. When the switch was made to the Gregorian calendar, January 1 became the first day of the year. This gives us a problem when a date is written as 15th day, 7th mo., 1700. In 1700, the seventh month of the year was not July; it was September. So this date would be 15 September 1700. Many beginning researchers get trapped in the pitfall of recording the wrong month for such a date. Many times, when I have been doing research in various sources for a particular event, I have found a two month difference in the date. Right away, I know I've run into this problem of misinterpretation of dates.

...
 
ah well, nobody's perfect except me.

Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top