This is a discussion on Validate a date to be in the format dd/mm/yyyy within the HTML, CSS and Javascript Coding Techniques forums, part of the Web Development category; Hi all... Can anyone tell me how can i validate a date to be in the format dd/mm/yyyy. ...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
|
#1
| |||
| |||
| Hi all... Can anyone tell me how can i validate a date to be in the format dd/mm/yyyy. Thanks in advance... |
|
#2
| |||
| |||
| Hi all... <SCRIPT LANGUAGE="JavaScript"> function y2k(number) { return (number < 1000) ? number + 1900 : number; } var reason = ''; function isValidDate (myDate,sep) { // checks if date passed is in valid dd/mm/yyyy format if (myDate.length == 10) { if (myDate.substring(2,3) == sep && myDate.substring(5,6) == sep) { var date = myDate.substring(0,2); var month = myDate.substring(3,5); var year = myDate.substring(6,10); var test = new Date(year,month-1,date); if (year == y2k(test.getYear()) && (month-1 == test.getMonth()) && (date == test.getDate())) { reason = ''; return true; } else { reason = 'valid format but an invalid date'; return false; } } else { reason = 'invalid spearators'; return false; } } else { reason = 'invalid length'; return false; } } function tellMeIfInvalid(myDate) { if (isValidDate(myDate,'/')) document.write(myDate + ' = valid date<BR>'); else document.write(myDate + ' = ' + reason + '<BR>'); } tellMeIfInvalid('21/02/1999'); tellMeIfInvalid('2190291999'); tellMeIfInvalid('2/02/1999'); tellMeIfInvalid('21/2/1999'); tellMeIfInvalid('21/02/199'); tellMeIfInvalid('32/02/1999'); tellMeIfInvalid('21/02/1999'); </SCRIPT> Thanks... |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Show all the dates between two dates in YYYY-MM-DD format | itbarota | HTML, CSS and Javascript Coding Techniques | 2 | 03-11-2008 08:05 PM |
| Date format yymmdd | itbarota | HTML, CSS and Javascript Coding Techniques | 2 | 03-03-2008 08:07 PM |
| How can I show the documents last modified date in the format: month, date, year, tim | Pvinothkumar | HTML, CSS and Javascript Coding Techniques | 3 | 12-28-2007 04:58 AM |
| How do I format the Last-Modified date with javascript? | Pvinothkumar | HTML, CSS and Javascript Coding Techniques | 1 | 10-12-2007 10:17 PM |
| How to convert a date to a globally accepted date format, in DOT NET 2005? | Archer | C# Programming | 1 | 07-25-2007 02:21 AM |
Our Partners |