Solved

Data validation - YYYY/MM/DD format

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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…

752 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