Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 862
  • Last Modified:

Data validation - YYYY/MM/DD format

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
Deenee
Asked:
Deenee
  • 2
1 Solution
 
callrsCommented:
Yes. Give me another half hour. Working on it  : )
0
 
callrsCommented:
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
 
DeeneeAuthor Commented:
Perfect!
Just what I needed, Thank you!!
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now