Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Data validation - YYYY/MM/DD format

Posted on 2006-06-09
3
Medium Priority
?
851 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

670 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