DATE URGENT!!!

i have the following date checking script which does so onkeyup, the date format i use is mm/dd/yyyy format.
i have evrything going on well except that when the user enters tomorrows date or any future date i have to check for that.
suppose if today he enters 11/16/2004 it shud throw an error.
pls help!!
Thanks
in Advance


// Check browser version
var isNav4 = false, isNav5 = false, isIE4 = false
var strSeperator = "/";
// If you are using any Java validation on the back side you will want to use the / because
// Java date validations do not recognize the dash as a valid date separator.
var vDateType = 3; // Global value for type of date format
//                1 = mm/dd/yyyy
//                2 = yyyy/dd/mm  (Unable to do date check at this time)
//                3 = dd/mm/yyyy
var vYearType = 4; //Set to 2 or 4 for number of digits in the year for Netscape
var vYearLength = 4; // Set to 4 if you want to force the user to enter 4 digits for the year before validating.
var err = 0; // Set the error code to a default of zero
if(navigator.appName == "Netscape") {
if (navigator.appVersion < "5") {
isNav4 = true;
isNav5 = false;
}
else
if (navigator.appVersion > "4") {
isNav4 = false;
isNav5 = true;
   }
}
else {
isIE4 = true;
}
function DateFormat(vDateName, vDateValue, e, dateCheck, dateType) {
vDateType = dateType;
// vDateName = object name
// vDateValue = value in the field being checked
// e = event
// dateCheck
// True  = Verify that the vDateValue is a valid date
// False = Format values being entered into vDateValue only
// vDateType
// 1 = mm/dd/yyyy
// 2 = yyyy/mm/dd
// 3 = dd/mm/yyyy
//Enter a tilde sign for the first number and you can check the variable information.
if (vDateValue == "~") {
alert("AppVersion = "+navigator.appVersion+" \nNav. 4 Version = "+isNav4+" \nNav. 5 Version = "+isNav5+" \nIE Version = "+isIE4+" \nYear Type = "+vYearType+" \nDate Type = "+vDateType+" \nSeparator = "+strSeperator);
vDateName.value = "";
vDateName.focus();
return true;
}
var whichCode = (window.Event) ? e.which : e.keyCode;
// Check to see if a seperator is already present.
// bypass the date if a seperator is present and the length greater than 8
if (vDateValue.length > 8 && isNav4) {
if ((vDateValue.indexOf("-") >= 1) || (vDateValue.indexOf("/") >= 1))
return true;
}
//Eliminate all the ASCII codes that are not valid
var alphaCheck = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/-";
if (alphaCheck.indexOf(vDateValue) >= 1) {
if (isNav4) {
vDateName.value = "";
vDateName.focus();
vDateName.select();
return false;
}
else {
vDateName.value = vDateName.value.substr(0, (vDateValue.length-1));
return false;
   }
}
if (whichCode == 8) //Ignore the Netscape value for backspace. IE has no value
return false;
else {
//Create numeric string values for 0123456789/
//The codes provided include both keyboard and keypad values
var strCheck = '47,48,49,50,51,52,53,54,55,56,57,58,59,95,96,97,98,99,100,101,102,103,104,105';
if (strCheck.indexOf(whichCode) != -1) {
if (isNav4) {
if (((vDateValue.length < 6 && dateCheck) || (vDateValue.length == 7 && dateCheck)) && (vDateValue.length >=1)) {
alert("You must enter a value in the mm/dd/yyyy format for the Date field.");
vDateName.value = "";
vDateName.focus();
vDateName.select();
return false;
}
if (vDateValue.length == 6 && dateCheck) {
var mDay = vDateName.value.substr(2,2);
var mMonth = vDateName.value.substr(0,2);
var mYear = vDateName.value.substr(4,4)
//Turn a two digit year into a 4 digit year
if (mYear.length == 2 && vYearType == 4) {
var mToday = new Date();
//If the year is greater than 30 years from now use 19, otherwise use 20
var checkYear = mToday.getFullYear() + 30;
var mCheckYear = '20' + mYear;
if (mCheckYear >= checkYear)
mYear = '19' + mYear;
else
mYear = '20' + mYear;
}
var vDateValueCheck = mMonth+strSeperator+mDay+strSeperator+mYear;
if (!dateValid(vDateValueCheck)) {
alert("You must enter a value in the mm/dd/yyyy format for the Date field.");
vDateName.value = "";
vDateName.focus();
vDateName.select();
return false;
}
return true;
}
else {
// Reformat the date for validation and set date type to a 1
if (vDateValue.length >= 8  && dateCheck) {
if (vDateType == 1) // mmddyyyy
{
var mDay = vDateName.value.substr(2,2);
var mMonth = vDateName.value.substr(0,2);
var mYear = vDateName.value.substr(4,4)
vDateName.value = mMonth+strSeperator+mDay+strSeperator+mYear;
}
if (vDateType == 2) // yyyymmdd
{
var mYear = vDateName.value.substr(0,4)
var mMonth = vDateName.value.substr(4,2);
var mDay = vDateName.value.substr(6,2);
vDateName.value = mYear+strSeperator+mMonth+strSeperator+mDay;
}
if (vDateType == 3) // ddmmyyyy
{
var mMonth = vDateName.value.substr(2,2);
var mDay = vDateName.value.substr(0,2);
var mYear = vDateName.value.substr(4,4)
vDateName.value = mDay+strSeperator+mMonth+strSeperator+mYear;
}
//Create a temporary variable for storing the DateType and change
//the DateType to a 1 for validation.
var vDateTypeTemp = vDateType;
vDateType = 1;
var vDateValueCheck = mMonth+strSeperator+mDay+strSeperator+mYear;
if (!dateValid(vDateValueCheck)) {
alert("You must enter a value in the mm/dd/yyyy format for the Date field.");
vDateType = vDateTypeTemp;
vDateName.value = "";
vDateName.focus();
vDateName.select();
return false;
}
vDateType = vDateTypeTemp;
return true;
}
else {
if (((vDateValue.length < 8 && dateCheck) || (vDateValue.length == 9 && dateCheck)) && (vDateValue.length >=1)) {
alert("You must enter a value in the mm/dd/yyyy format for the Date field.");
vDateName.value = "";
vDateName.focus();
vDateName.select();
return false;
         }
      }
   }
}
else {
// Non isNav Check
if (((vDateValue.length < 8 && dateCheck) || (vDateValue.length == 9 && dateCheck)) && (vDateValue.length >=1)) {
      if(document.Proceed.RATING_DATE_TYPE[1].checked == true)
            alert("You must enter a value in the mm/dd/yyyy format for both the To and From fields.");
      else
            alert("You must enter a value in the mm/dd/yyyy format for the Date field.");
      
vDateName.value = "";
vDateName.focus();
return true;
}
// Reformat date to format that can be validated. mm/dd/yyyy
if (vDateValue.length >= 8 && dateCheck) {
// Additional date formats can be entered here and parsed out to
// a valid date format that the validation routine will recognize.
if (vDateType == 1) // mm/dd/yyyy
{
var mMonth = vDateName.value.substr(0,2);
var mDay = vDateName.value.substr(3,2);
var mYear = vDateName.value.substr(6,4)
}
if (vDateType == 2) // yyyy/mm/dd
{
var mYear = vDateName.value.substr(0,4)
var mMonth = vDateName.value.substr(5,2);
var mDay = vDateName.value.substr(8,2);
}
if (vDateType == 3) // dd/mm/yyyy
{
var mDay = vDateName.value.substr(0,2);
var mMonth = vDateName.value.substr(3,2);
var mYear = vDateName.value.substr(6,4)
}
if (vYearLength == 4) {
if (mYear.length < 4) {
      if(document.Proceed.RATING_DATE_TYPE[1].checked == true)
            alert("You must enter a value in the mm/dd/yyyy format for both the To and From fields.");
      else
            alert("You must enter a value in the mm/dd/yyyy format for the Date field.");
vDateName.value = "";
vDateName.focus();
return true;
   }
}
// Create temp. variable for storing the current vDateType
var vDateTypeTemp = vDateType;
// Change vDateType to a 1 for standard date format for validation
// Type will be changed back when validation is completed.
vDateType = 1;
// Store reformatted date to new variable for validation.
var vDateValueCheck = mMonth+strSeperator+mDay+strSeperator+mYear;
if (mYear.length == 2 && vYearType == 4 && dateCheck) {
//Turn a two digit year into a 4 digit year
var mToday = new Date();
//If the year is greater than 30 years from now use 19, otherwise use 20
var checkYear = mToday.getFullYear() + 30;
var mCheckYear = '20' + mYear;
if (mCheckYear >= checkYear)
mYear = '19' + mYear;
else
mYear = '20' + mYear;
vDateValueCheck = mMonth+strSeperator+mDay+strSeperator+mYear;
// Store the new value back to the field.  This function will
// not work with date type of 2 since the year is entered first.
if (vDateTypeTemp == 1) // mm/dd/yyyy
vDateName.value = mMonth+strSeperator+mDay+strSeperator+mYear;
if (vDateTypeTemp == 3) // dd/mm/yyyy
vDateName.value = mDay+strSeperator+mMonth+strSeperator+mYear;
}
if (!dateValid(vDateValueCheck)) {
//alert("You must enter a value in the mm/dd/yyyy format for the Date field.");
vDateType = vDateTypeTemp;
vDateName.value = "";
vDateName.focus();
return true;
}
vDateType = vDateTypeTemp;
return true;
}
else {
if (vDateType == 1) {
if (vDateValue.length == 2) {
vDateName.value = vDateValue+strSeperator;
}
if (vDateValue.length == 5) {
vDateName.value = vDateValue+strSeperator;
   }
}
if (vDateType == 2) {
if (vDateValue.length == 4) {
vDateName.value = vDateValue+strSeperator;
}
if (vDateValue.length == 7) {
vDateName.value = vDateValue+strSeperator;
   }
}
if (vDateType == 3) {
if (vDateValue.length == 2) {
vDateName.value = vDateValue+strSeperator;
}
if (vDateValue.length == 5) {
vDateName.value = vDateValue+strSeperator;
   }
}
return true;
   }
}
if (vDateValue.length == 10&& dateCheck) {
if (!dateValid(vDateName)) {
// Un-comment the next line of code for debugging the dateValid() function error messages
//alert(err);  
//alert("You must enter a value in the mm/dd/yyy format for the Date field.");
vDateName.focus();
vDateName.select();
   }
}
return false;
}
else {
// If the value is not in the string return the string minus the last
// key entered.
if (isNav4) {
vDateName.value = "";
vDateName.focus();
vDateName.select();
return false;
}
else
{
vDateName.value = vDateName.value.substr(0, (vDateValue.length-1));
return false;
         }
      }
   }
}
function dateValid(objName) {
var strDate;
var strDateArray;
var strDay;
var strMonth;
var strYear;
var intday;
var intMonth;
var intYear;
var booFound = false;
var datefield = objName;
var strSeparatorArray = new Array("-"," ","/",".");
var intElementNr;
// var err = 0;
var strMonthArray = new Array(12);
strMonthArray[0] = "Jan";
strMonthArray[1] = "Feb";
strMonthArray[2] = "Mar";
strMonthArray[3] = "Apr";
strMonthArray[4] = "May";
strMonthArray[5] = "Jun";
strMonthArray[6] = "Jul";
strMonthArray[7] = "Aug";
strMonthArray[8] = "Sep";
strMonthArray[9] = "Oct";
strMonthArray[10] = "Nov";
strMonthArray[11] = "Dec";
//strDate = datefield.value;
strDate = objName;
if (strDate.length < 1) {
return true;
}
for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) {
if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) {
strDateArray = strDate.split(strSeparatorArray[intElementNr]);
if (strDateArray.length != 3) {
err = 1;
return false;
}
else {
strDay = strDateArray[0];
strMonth = strDateArray[1];
strYear = strDateArray[2];
}
booFound = true;
   }
}
if (booFound == false) {
if (strDate.length>5) {
strDay = strDate.substr(0, 2);
strMonth = strDate.substr(2, 2);
strYear = strDate.substr(4);
   }
}
//Adjustment for short years entered
if (strYear.length == 2) {
strYear = '20' + strYear;
}
strTemp = strDay;
strDay = strMonth;
strMonth = strTemp;
intday = parseInt(strDay, 10);
if (isNaN(intday)) {
err = 2;
return false;
}
intMonth = parseInt(strMonth, 10);
if (isNaN(intMonth)) {
for (i = 0;i<12;i++) {
if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase()) {
intMonth = i+1;
strMonth = strMonthArray[i];
i = 12;
   }
}
if (isNaN(intMonth)) {
err = 3;
return false;
   }
}
intYear = parseInt(strYear, 10);
if (isNaN(intYear)) {
err = 4;
return false;
}
var YY = (new Date()).getFullYear();
 if(intYear>YY) {
 
//err = 11;
alert("The date you have selected is not valid. Please try again.");
return false;
}
if (intMonth>12 || intMonth<1) {
alert("You must enter a value in the mm/dd/yyyy format for the Date field.a");
return false;
}
if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) {
alert("You must enter a value in the mm/dd/yyyy format for the Date field.");
return false;
}
if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) {
alert("You must enter a value in the mm/dd/yyyy format for the Date field.");
return false;
}
if (intMonth == 2) {
if (intday < 1) {
alert("You must enter a value in the mm/dd/yyyy format for the Date field.");
return false;
}
if (LeapYear(intYear) == true) {
if (intday > 29) {
alert("You must enter a value in the mm/dd/yyyy format for the Date field.");
return false;
   }
}
else {
if (intday > 28) {
alert("You must enter a value in the mm/dd/yyyy format for the Date field.");
return false;
      }
   }
}
return true;
}
function LeapYear(intYear) {
if (intYear % 100 == 0) {
if (intYear % 400 == 0) { return true; }
}
else {
if ((intYear % 4) == 0) { return true; }
}
return false;
}
LVL 25
archrajanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ZvonkoSystems architectCommented:
How about this:

<html>
<head>
<script>
function enterDate(theField){
  var fMsg = "Please use this Formet: MM/DD/YYYY";
  var dV = theField.value;
  var vP = dV.match(/^(\d{1,2})\/?$/);
  if(!vP) vP = dV.match(/^(\d{1,2})\/(\d{1,2})\/?$/);
  if(!vP) vP = dV.match(/^(\d{1,2})\/(\d{1,2})\/(\d{1,4})$/);
  if(vP!=null){
    if(vP[1]>12) return chkMsg(theField,"Moth greater then 12\n"+fMsg);
    if(vP[2]>31) return chkMsg(theField,"Date greater then 31\n"+fMsg);
    if(vP[3]&&"200".indexOf(vP[3].substr(0,3))!=0) return chkMsg(theField,"Year can be only betwean 2004 and 2005");
    if(vP[3]>2005) return chkMsg(theField,"Year greater then 2005\n"+fMsg);
    dV  = chkAdd(vP[1],2,2,"/");
    dV += chkAdd(vP[2],2,3,"/");
    if(dV.length==6&&!vP[3])vP[3]="200";
    dV += chkAdd(vP[3],4,9,"");
    if(dV!=theField.value) theField.value=dV;
    if(vP[3]>""){
      var aDate = new Date();
      var vDate = new Date(dV);
      if(aDate<vDate){
        return chkMsg(theField, "Date is in the Future.");
      }
      if(vDate.getDate()!=vP[2]||vDate.getMonth()!=vP[1]-1){
        return chkMsg(theField, fMsg);
      }
    }
   
  } else {
    if(dV>"") chkMsg(theField,fMsg);
  }
}
function chkAdd(theVal,fLen,lVal,sChar){
  if(!theVal) return "";
  if(theVal>lVal&&theVal.length==1) return "0"+theVal+sChar;
  if(theVal.length==fLen) return theVal+sChar;
  return theVal;
}
var wVal="";
function chkMsg(theObj,theMsg){
  if(wVal!=theObj.value){
    alert(theMsg);
    theObj.select();
    theObj.focus();
  }
  wVal=theObj.value;
}
</script>
</head>
<body onLoad="document.forms[0].startDate.focus()">
<form>
Date:
<input type=text name="startDate" size="10" maxLength="10" onKeyUp="enterDate(this)">
</form>
</body>
</html>

0
archrajanAuthor Commented:
this does not check for leap year and months that can have only 30 days or so...
can u just modify the script  i already have...
0
ZvonkoSystems architectCommented:
Do you think so or did you test my code?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

ZvonkoSystems architectCommented:
This version removes two unnecessary messages:

<html>
<head>
<script>
function enterDate(theField){
  var fMsg = "Please use this Formet: MM/DD/YYYY";
  var dV = theField.value;
  var vP = dV.match(/^(\d{1,2})\/?$/);
  if(!vP) vP = dV.match(/^(\d{1,2})\/(\d{1,2})\/?$/);
  if(!vP) vP = dV.match(/^(\d{1,2})\/(\d{1,2})\/(\d{1,4})$/);
  if(vP!=null){
    if(vP[1]>12) return chkMsg(theField,"Month greater then 12\n"+fMsg);
    if(vP[2]>31) return chkMsg(theField,"Date greater then 31\n"+fMsg);
    dV  = chkAdd(vP[1],2,1,"/");
    dV += chkAdd(vP[2],2,3,"/");
    if(dV.length==6&&!vP[3])vP[3]="200";
    dV += chkAdd(vP[3],4,9,"");
    if(dV!=theField.value) theField.value=dV;
    if(vP[3]>""){
      var aDate = new Date();
      var vDate = new Date(dV);
      if(aDate<vDate){
        return chkMsg(theField, "Date is in the Future.");
      }
      if(vDate.getDate()!=vP[2]||vDate.getMonth()!=vP[1]-1){
        return chkMsg(theField, fMsg);
      }
    }
   
  } else {
    if(dV>"") chkMsg(theField,fMsg);
  }
}
function chkAdd(theVal,fLen,lVal,sChar){
  if(!theVal) return "";
  if(theVal>lVal&&theVal.length==1) return "0"+theVal+sChar;
  if(theVal.length==fLen) return theVal+sChar;
  return theVal;
}
var wVal="";
function chkMsg(theObj,theMsg){
  if(wVal!=theObj.value){
    alert(theMsg);
    theObj.select();
    theObj.focus();
  }
  wVal=theObj.value;
}
</script>
</head>
<body onLoad="document.forms[0].startDate.focus()">
<form>
Date:
<input type=text name="startDate" size="10" maxLength="10" onKeyUp="enterDate(this)">
</form>
</body>
</html>

0
GwynforWebCommented:
try this

<script>
function enterDate(date){
//check valid format
 if (!/^\d{1,2}\/\d{1,2}\/\d{1,4}$/.test(date)){ alert ('Invalid format');return false}

//check valid date
 date=date.split('/')
 d=new Date(date[2],date[0]-1,date[1])
 if (!( 1*date[1]==d.getDate() && 1*date[0]==(d.getMonth()+1) && 1*date[2]==d.getYear()))
 { alert('Invalid date');return false}

//check in past
 today=new Date()
 d==new Date(d.getYear(),1*d.getMonth()-1,d.getDate())
 if (d.getTime() > today.getTime()){ alert ('Date is in future');return false}

// All ok retrun true
 return true
}
</script>

<form>
  <p>Date: <input type="text" name="startDate" size="10" maxLength="10" onblur="enterDate(this.value)"> </p>
</form>
0
GwynforWebCommented:
shld read       d=new Date(d.getYear(),1*d.getMonth()-1,d.getDate())      not    d==new Date(d.getYear(),1*d.getMonth()-1,d.getDate())

(still works as is  though)
0
reginabCommented:
<SCRIPT LANGUAGE=javascript>
<!--
function compareDates()
{
     datNow = new Date()
     datFirst = new Date("1 Jan 2003")
     datSecond = new Date("5 Jan 1999")
     
     document.write(datNow + "<BR>");
     document.write(datFirst + "<BR>");
     document.write(datSecond + "<BR>");
     
     numNow = Date.UTC(datNow.getYear(), datNow.getMonth(), datNow.getDay())
     numFirst = Date.UTC(datFirst.getYear(), datFirst.getMonth(), datFirst.getDay())
     numSecond = Date.UTC(datSecond.getYear(), datSecond.getMonth(), datSecond.getDay())

     document.write(numNow + "<BR>");
     document.write(numFirst + "<BR>");
     document.write(numSecond + "<BR>");

     if(numNow-numFirst<0){
       alert("The first date must be no later than today");
     }
     if(numNow-numSecond<0){
       alert("The second date must be no later than today");
     }

}
//-->
</SCRIPT>
0
archrajanAuthor Commented:
zvonko your script adds 200 by default after u enter the day and month
but as far as my requirement is concerned the past day can be anything unless its a four digit number.
0
ZvonkoSystems architectCommented:
Ok, then check this:

<html>
<head>
<script>
function enterDate(theField){
  var fMsg = "Please use this Formet: MM/DD/YYYY";
  var dV = theField.value;
  var vP = dV.match(/^(\d{1,2})\/?$/);
  if(!vP) vP = dV.match(/^(\d{1,2})\/(\d{1,2})\/?$/);
  if(!vP) vP = dV.match(/^(\d{1,2})\/(\d{1,2})\/(\d{1,4})$/);
  if(vP!=null){
    if(vP[1]>12) return chkMsg(theField,"Month greater then 12\n"+fMsg);
    if(vP[2]>31) return chkMsg(theField,"Date greater then 31\n"+fMsg);
    dV  = chkAdd(vP[1],2,1,"/");
    dV += chkAdd(vP[2],2,3,"/");
    dV += chkAdd(vP[3],4,9,"");
    if(dV!=theField.value) theField.value=dV;
    if(vP[3]&&vP[3].length==4){
      var aDate = new Date();
      var vDate = new Date(dV);
      if(aDate<vDate){
        return chkMsg(theField, "Date is in the Future.");
      }
      if(vDate.getDate()!=vP[2]||vDate.getMonth()!=vP[1]-1){
        return chkMsg(theField, fMsg);
      }
    }
   
  } else {
    if(dV>"") chkMsg(theField,fMsg);
  }
}
function chkAdd(theVal,fLen,lVal,sChar){
  if(!theVal) return "";
  if(theVal>lVal&&theVal.length==1) return "0"+theVal+sChar;
  if(theVal.length==fLen) return theVal+sChar;
  return theVal;
}
var wVal="";
function chkMsg(theObj,theMsg){
  if(wVal!=theObj.value){
    alert(theMsg);
    theObj.select();
    theObj.focus();
  }
  wVal=theObj.value;
}
</script>
</head>
<body onLoad="document.forms[0].startDate.focus()">
<form>
Date:
<input type=text name="startDate" size="10" maxLength="10" onKeyUp="enterDate(this)">
</form>
</body>
</html>

0
archrajanAuthor Commented:
if the user just enters 11/10/199 it does not say an error.
also when the / comes automatically when i click backspace its not getting deleted

0
ZvonkoSystems architectCommented:
Come on! What date do you need in year 199????
By the way, was the year 199 a leap year ;-)
And to delete characters you destroy the complette logic!
What should happen in your scenario if the user deletes a slash character?
The use has to retype the complette date if he wants to change the date.
That's the price for automatic adding of zeros and slashes.
0
archrajanAuthor Commented:
but the script i posted deletes the slash character
199 is not a valid year for me.
the date shud be in mm/dd/yyyy format.
four characters in the year
0
ZvonkoSystems architectCommented:
I do not understand. What should the script do with year 199? Kill the form submit? That's easy.
But now I have to leave and will be online again in four hours?
So long,
Zvonko

0
GwynforWebCommented:
I think this does it all

<script>
function enterDate(obj,submit){
date=obj.value
if (!submit){
//If not a submission check than do keyup check
//Automatic slash addition
 obj.value=obj.value.replace(/^(\d{2})$/,'$1/')
 obj.value=obj.value.replace(/^(\d{2}\/\d{2})$/,'$1/')
 

//Pad with zeros
 obj.value=date.replace(/^(\d{2}\/)(\d){1}\/$/,"$10$2/")
 obj.value=date.replace(/\/(\d)\/$/,"/0$1/")

//Check key entry
 len=date.length
 test1=test2=test3=true
 if ((len <4)) test1=(/^\d{1,2}\/?$/.test(date)  )
 if (len <7 && len >=4) test2=(/^\d{1,2}\/\d{1,2}\/?$/.test(date) )
 if (len >6) test3=(/^\d{1,2}\/\d{1,2}\/\d{1,4}$/.test(date) )
 if (!test1 || !test2 || !test3) {obj.value=obj.value.replace(/.$/,'');return false}
 if (len <10) return true
 
}


//Check valid date

 if (!/^(\d{2})\/(\d{2})\/(\d{4})$/.test(date)) return false
 date=date.split('/')
 d=new Date(date[2],date[0]-1,date[1])
 if (!( 1*date[1]==d.getDate() && 1*date[0]==(d.getMonth()+1) && 1*date[2]==d.getYear()))
 { alert('Invalid date');return false}

//check in past
 today=new Date()
 d==new Date(d.getYear(),1*d.getMonth()-1,d.getDate())
 if (d.getTime() > today.getTime()){ alert ('Date is in future');return false}

// All ok retrun true
 return true
}
</script>

<form onsubmit="return enterDate(this.startDate,1)">
  <p>Date: <input type="text" name="startDate" size="10" maxLength="10"
  onkeyup="enterDate(this,0)" onblur="enterDate(this,0)"> </p>
</form>
0
GwynforWebCommented:
sorry a couple of typos here it is

<script>
function enterDate(obj,submit){
date=obj.value
if (!submit){
//If not a submission check than do keyup check
//Automatic slash addition

 if ( /^(\d{2})$/.test(date) ){
  obj.value=obj.value.replace(/^(\d{2})$/,'$1/');
  return}

if (/^(\d{2}\/\d{2})$/.test(date)){
 obj.value=obj.value.replace(/^(\d{2}\/\d{2})$/,'$1/')
 return
}

//Pad with zeros
 obj.value=date.replace(/^(\d{2}\/)(\d){1}\/$/,"$10$2/")
 obj.value=date.replace(/^(\d)\/$/,"0$1/")

//Check key entry
 len=date.length
 test1=test2=test3=true
 if ((len <4)) test1=(/^\d{1,2}\/?$/.test(date)  )
 if (len <7 && len >=4) test2=(/^\d{1,2}\/\d{1,2}\/?$/.test(date) )
 if (len >6) test3=(/^\d{1,2}\/\d{1,2}\/\d{1,4}$/.test(date) )
 if (!test1 || !test2 || !test3) {obj.value=obj.value.replace(/.$/,'');return false}
 if (len <10) return true
 
}


//Check valid date

 if (!/^(\d{2})\/(\d{2})\/(\d{4})$/.test(date)) return false
 date=date.split('/')
 d=new Date(date[2],date[0]-1,date[1])
 if (!( 1*date[1]==d.getDate() && 1*date[0]==(d.getMonth()+1) && 1*date[2]==d.getYear()))
 { alert('Invalid date');return false}

//check in past
 today=new Date()
 d=new Date(d.getYear(),1*d.getMonth()-1,d.getDate())
 if (d.getTime() > today.getTime()){ alert ('Date is in future');return false}

// All ok retrun true
 return true
}
</script>

<form onsubmit="return enterDate(this.startDate,1)">
  <p>Date: <input type="text" name="startDate" size="10" maxLength="10"
  onkeyup="enterDate(this,0)" onblur="enterDate(this,0)"> </p>
<input type=submit>
</form>

0
ZvonkoSystems architectCommented:
What about this:

<html>
<head>
<script>
function enterDate(theField){
  var fMsg = "Please use this Formet: MM/DD/YYYY";
  var dV = theField.value;
  var vP = dV.match(/^(\d{1,2})\/?$/);
  if(!vP) vP = dV.match(/^(\d{1,2})\/(\d{1,2})\/?$/);
  if(!vP) vP = dV.match(/^(\d{1,2})\/(\d{1,2})\/(\d{1,4})$/);
  if(vP!=null){
    if(vP[1]>12) return chkMsg(theField,"Month greater then 12\n"+fMsg);
    if(vP[2]>31) return chkMsg(theField,"Date greater then 31\n"+fMsg);
    dV  = chkAdd(vP[1],2,1,"/");
    dV += chkAdd(vP[2],2,3,"/");
    dV += chkAdd(vP[3],4,9,"");
    if(dV!=theField.value) theField.value=dV;
    if(vP[3]&&vP[3].length==4){
      var aDate = new Date();
      var vDate = new Date(dV);
      if(aDate<vDate){
        return chkMsg(theField, "Date is in the Future.");
      }
      if(vDate.getDate()!=vP[2]||vDate.getMonth()!=vP[1]-1){
        return chkMsg(theField, fMsg);
      }
      return true;
    }
  } else {
    if(dV>"") chkMsg(theField,fMsg);
  }
  return false;
}
function chkAdd(theVal,fLen,lVal,sChar){
  if(!theVal) return "";
  if(theVal>lVal&&theVal.length==1) return "0"+theVal+sChar;
  if(theVal.length==fLen) return theVal+sChar;
  return theVal;
}
var wVal="";
function chkMsg(theObj,theMsg){
  if(wVal!=theObj.value){
    alert(theMsg);
    theObj.select();
    theObj.focus();
  }
  wVal=theObj.value;
  return false
}
</script>
</head>
<body onLoad="document.forms[0].startDate.focus()">
<form onSubmit="return enterDate(this.startDate)">
Date:
<input type=text name="startDate" size="10" maxLength="10" onKeyUp="enterDate(this)">
<input type=submit>
</form>
</body>
</html>


0
archrajanAuthor Commented:
i want the same code as mine modified to check for future date
0
ozoCommented:
To check for future date, you can just put the day and today in yyyy/mm/dd form and do a string compare
0
ZvonkoSystems architectCommented:
You want me to analyze that nonsesse code???
Look here:
function DateFormat(vDateName, vDateValue, e, dateCheck, dateType) {
vDateType = dateType;
// vDateName = object name
//....
vDateName.value = "";

First parameter is "object name". To that name you assign a empty string. And for the field value does the function need a separate second parameter???
Come on. You do not know how to use the Date() object and the RegExp object in JavaScript and therefore you need manual LeapYear calculation with divide by 400!
0
archrajanAuthor Commented:
OK CAN U JUST GIVE ME A CODE THAT will just validate the future date? and not any thing else.
0
ZvonkoSystems architectCommented:
It's easy like this:

<script>

   var cDate = new Date("11/17/2004");
   var aDate = new Date();
   if(cDate>aDate){
     alert("The date is in the future");
   }

</script>

0
ZvonkoSystems architectCommented:
And if you have to check other date formats then the MM/DD/YYYY format, then use this:

<script>

   var cDate = new Date(2004, 11, 17);
   var aDate = new Date();
   if(cDate>aDate){
     alert("The date is in the future");
   }

</script>


Of course you can use in this and in the previous example vars for new Date() parameters.

0
ZvonkoSystems architectCommented:
Sorry, one typo. In the last example you need this for November, the 17th:

<script>

   var cDate = new Date(2004, 10, 17);
   var aDate = new Date();
   if(cDate>aDate){
     alert("The date is in the future");
   }

</script>

Because the month index start at zero for January.

0
ZvonkoSystems architectCommented:
And to see the script objects simply add an alert() line:

<script>

   var cDate = new Date(2004, 10, 17);
   var aDate = new Date();

alert(cDate);

   if(cDate>aDate){
     alert("The date is in the future");
   }
 

</script>
0
archrajanAuthor Commented:
one more doubt
if the user enters 11/11/2004 in the textfield
how do i get the date from the field.
suppose cdate
0
ZvonkoSystems architectCommented:
Like this:

<script>

   var cDate = new Date(document.forms[0].startDate.value);
   var aDate = new Date();

alert(cDate);

   if(cDate>aDate){
     alert("The date is in the future");
   }
 

</script>

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
archrajanAuthor Commented:
hmmm.. this is what i was lookin for... why u giving me everything else before..
heheheh.. anyways
thanks a bunch!!!
0
ZvonkoSystems architectCommented:
That's the biggest challenge in programming: understand user's requirements ;-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.