Solved

Data validation - YYYY/MM/DD format

Posted on 2006-06-09
3
810 Views
Last Modified: 2008-02-01
Hi!
Is it possible to make this script validate YYYY/MM/DD format instead?

Thanks,
Deenee
--------------------------------------------------------------------------------
<!-- Original:  Sandeep V. Tamhankar (stamhankar@hotmail.com) -->
<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- Begin
function isValidDate(dateStr) {
// Checks for the following valid date formats:
// MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY
// Also separates date into month, day, and year variables

var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;

// To require a 4 digit year entry, use this line instead:
// var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;

var matchArray = dateStr.match(datePat); // is the format ok?
if (matchArray == null) {
alert("Date is not in a valid format.")
return false;
}
month = matchArray[1]; // parse date into variables
day = matchArray[3];
year = matchArray[4];
if (month < 1 || month > 12) { // check month range
alert("Month must be between 1 and 12.");
return false;
}
if (day < 1 || day > 31) {
alert("Day must be between 1 and 31.");
return false;
}
if ((month==4 || month==6 || month==9 || month==11) && day==31) {
alert("Month "+month+" doesn't have 31 days!")
return false
}
if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day>29 || (day==29 && !isleap)) {
alert("February " + year + " doesn't have " + day + " days!");
return false;
   }
}
return true;  // date is valid
}
//  End -->
--------------------------------------------------------------------------------------
0
Comment
Question by:Deenee
  • 2
3 Comments
 
LVL 30

Expert Comment

by:callrs
ID: 16868589
Yes. Give me another half hour. Working on it  : )
0
 
LVL 30

Accepted Solution

by:
callrs earned 125 total points
ID: 16868823
Ok just copy & paste this code into a new text file, save it as c:\temp\date.htm
Then type into your browser Address bar: c:\temp\date.htm
Enjoy :

<html>
<!-- Original:  Sandeep V. Tamhankar (stamhankar@hotmail.com) -->
<!-- Revised & set to the more sensible YYYY-MM-DD format by: -->
<!--                Ravinder Singh ('wiz' at the quickmacros forum) -->
<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<script type="text/javascript">
function isValidDate(dateStr) {
// Checks for the valid date formats of:
// YYYY-MM-DD YYYY/MM/DD
// Also separates date into month, day, and year variables
// var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;
var datePat = /^(\d{4})(\/|-)(\d{1,2})\2(\d{1,2})$/;

// verify format
var matchArray = dateStr.match(datePat);
if (matchArray == null) {alert(dateStr+":  Not a valid format."); return false;}

// parse date into variables
year = matchArray[1]; month = matchArray[3];day = matchArray[4];

if (month < 1 || month > 12) {alert(dateStr+":  Month must be between 1 and 12."); return false;}

if (day < 1 || day > 31) {alert(dateStr+":  Day must be between 1 and 31."); return false;}

if ((month==4 || month==6 || month==9 || month==11) && day==31){
      alert(dateStr+"  Month "+month+" doesn't have 31 days!"); return false;}

// check for february 29th
if (month == 2) {
      var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
      if (day>29 || (day==29 && !isleap)) {
            alert(dateStr+":  February " + year + " doesn't have " + day + " days!");
            return false;}
      }
return true;  // date is valid
}

// Test the function
var theDate=prompt('Enter a date in YYYY-MM-DD format:');
var isValid=isValidDate(theDate);
if(isValid==true) alert(theDate+" OK");
</script>
</html>
0
 

Author Comment

by:Deenee
ID: 16869099
Perfect!
Just what I needed, Thank you!!
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question