JavaScript Script: detailed validation of dates within a range
This script is called JavaScript-Script-detailed-validation-of-dates-within-a-range.html
In searching the Internet, we found no evidence of a JavaScript Script that gives a detailed validation of dates within a range. We found scripts that indicated valid or invalid or alerted the user of bad formatting, but no detailed alerts checking every aspect of the input. Our script does this.
Starting with the JavaScript function validateit(), we define variables. And next we get the date
from the form using the document object model's dot notation. We use the length property to ensure the string length is 10. Note that before we return false after alerting you of the error details (denying submission of the date to your PHP script—whatever it is), we use the focus() method to keep the cursor in the text input box.
We next use the substring() method to check the number separators. Now we pull month, day, and year out of the input using the substring() method again. Then we validate or invalidate each, alerting you of error details. Note that 2012 to 2999 is our range. You may use your own range instead. Next we check if the months have valid numbers of days in them, alerting you of any error details. Note the Modulus (division remainder) operator that finds out if one of two numbers divides evenly into the other—or not.
Now we utilize regular expressions to ensure that the three numbers in the date are the right length and contain only digits. Using the search() method, we check each variable: yer, day, and month. If all these tests above are passed, we inform you that his/her date validated. In practice, it is assumed that you want the date input for a reason, and you will only give error messages, not validation messages, and you will send the script off to some PHP script with the validated date.
Note that there is an onsubmit event attribute in the form tag: onsubmit="return validateit()". It allows the JavaScript validateit() function to only submit if no errors are found in your input. The JavaScript action="#" should be replaced with the URL of your PHP script. The rest of the form is the standard stuff.
This script is called JavaScript-Script-detailed-validation-of-dates-within-a-range.html
<html>
<head>
<script language="javascript">
function validateit(){
var myDate,day,month,year;
var flag=0;
myDate=document.myForm.myDate.value;
if (myDate.length != 10) {alert("Inappropriate date format length; should be 10 characters");document.myForm.myDate.focus();return false;}
if (myDate.substring(2, 3) != '-' || myDate.substring(5, 6) != '-'){alert("Inappropriate number separator; should be hyphen");document.myForm.myDate.focus();return false;}
month = myDate.substring(0, 2);
day = myDate.substring(3, 5);
year = myDate.substring(6, 10);
if (month < 1 || month > 12) {alert("Inappropriate month");document.myForm.myDate.focus();return false;}
if (day < 1 || day > 31){alert("Inappropriate day");document.myForm.myDate.focus();return false;}
if (year < 2012 || year > 2999) {alert("Inappropriate year");document.myForm.myDate.focus();return false;}
if ((month ==9 || month ==4 || month ==6 || month ==11) && day==31){alert("Inappropriate September's or November's or April's or June's number of days");document.myForm.myDate.focus();return false;}
if(year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)) {flag=1;}
if (month ==2 && day>28+flag) {alert("Inappropriate number of February days");document.myForm.myDate.focus();return false;}
var re4digit=/^\d{4}$/; //regular expression defining a 4 digit number
if (year.search(re4digit)==-1){alert("Please enter a valid 4 digit number for year");document.myForm.myDate.focus();return false;}
var re2digit=/^\d{2}$/; //regular expression defining a 2 digit number
if (day.search(re2digit)==-1){alert("Please enter a valid 2 digit number for day");document.myForm.myDate.focus();return false;}
if (month.search(re2digit)==-1){alert("Please enter a valid 2 digit number for month");document.myForm.myDate.focus();return false;}
alert("validated: "+month+"-"+day+"-"+year);return true;}
</script>
</head>
<body>
<div style="position:absolute;left:200px;top:132px;z-index:99;width:600px;height:60px">
<form name="myForm" id="form" method="post" onsubmit="return validateit()" action="#" style="background-color:#ccc;width:600px;"><!--put your-date-needing-script.php as the action attribute-->
<center><table width="600px" height="60" border="1" align="center">
<tr>
<td>Date from 2012 to 2999 (mm-dd-yyyy): </td>
<td><input name="myDate" type="text" size="10" maxlength="10" value=""></td>
<td><input name="submit" type="submit" id="submit" value="submit"></td>
</tr>
</table><center></form></div>
</body>
</html>