Solved

Data validation - YYYY/MM/DD format

Posted on 2006-06-09
3
797 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now