Make a text box input mask?

  • vickriz
  • Novice
  • Novice
  • User avatar
  • Posts: 31
  • Loc: Phlippines

Post 3+ Months Ago

how can i make an input mask on textbox for a date mm/dd/yyyy. example input 07/16/1975 and then check whether the date of year is within the range of 1930 to 2003.
any help would appreciate..
thanks..
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • b_heyer
  • Web Master
  • Web Master
  • User avatar
  • Posts: 4581
  • Loc: Maryland

Post 3+ Months Ago

I think this is what you are looking for:


Code: [ Select ]
<?

//this is the method file of the post function for <form>

$year = substr("$_POST['date']", 6); //'date' is the name for the date field
if ($year >= 1930)
{
 if ($year <=2003)
 {
   echo("That is a valid year");
 } else {
   echo("That year has not happened yet!");
} else {
 echo("That year was before 1930");
}

?>
  1. <?
  2. //this is the method file of the post function for <form>
  3. $year = substr("$_POST['date']", 6); //'date' is the name for the date field
  4. if ($year >= 1930)
  5. {
  6.  if ($year <=2003)
  7.  {
  8.    echo("That is a valid year");
  9.  } else {
  10.    echo("That year has not happened yet!");
  11. } else {
  12.  echo("That year was before 1930");
  13. }
  14. ?>
  • vickriz
  • Novice
  • Novice
  • User avatar
  • Posts: 31
  • Loc: Phlippines

Post 3+ Months Ago

b_heyer wrote:
I think this is what you are looking for:


Code: [ Select ]
<?

//this is the method file of the post function for <form>

$year = substr("$_POST['date']", 6); //'date' is the name for the date field
if ($year >= 1930)
{
 if ($year <=2003)
 {
   echo("That is a valid year");
 } else {
   echo("That year has not happened yet!");
} else {
 echo("That year was before 1930");
}

?>
  1. <?
  2. //this is the method file of the post function for <form>
  3. $year = substr("$_POST['date']", 6); //'date' is the name for the date field
  4. if ($year >= 1930)
  5. {
  6.  if ($year <=2003)
  7.  {
  8.    echo("That is a valid year");
  9.  } else {
  10.    echo("That year has not happened yet!");
  11. } else {
  12.  echo("That year was before 1930");
  13. }
  14. ?>


Hi b_heyer good day.. thanks for your post reply. but im looking a javascript that will make input mask for my textbox. let say if i input this date 07/16/1975 it will check if the date input was valid. also, only the '/' was a valid separator for month, day and year.. any other suggestions or if you have some code like this??
  • RichB
  • Guru
  • Guru
  • User avatar
  • Posts: 1121
  • Loc: Boston

Post 3+ Months Ago

There's an input mask script at the javascript source. Download it by clicking and saving it:

http://javascript.internet.com/forms/dFilter.js

then your code would look something like this:

Code: [ Select ]
<html>
<head>
<script language="javascript">
function checkYear(){
var d = document.theForm.theDate.value;
var y = d.substr(d.lastIndexOf("/")+1,d.length-1);
    if(y >= 1930 && y <= 2003){
    return true; // submit the form
    }
alert('Please enter a year between 1930 and 2003');
document.theForm.theDate.value=d.substr(0,d.lastIndexOf("/")+1);
document.theForm.theDate.focus();
return false;
}
</script>
<script type='text/javascript' src='dFilter.js'></script>
</head>
<body>
<form name="theForm" action="" method="" onsubmit="return checkYear();">
<table>
<tr><td>Date:</td>
<td><input value="" type="text" name="theDate" size="15" onKeyDown="javascript:return dFilter (event.keyCode, this, '##/##/####');"></td>
</tr>
</table>
<input type="submit"><input type="reset">
</form>
</body>
</html>
  1. <html>
  2. <head>
  3. <script language="javascript">
  4. function checkYear(){
  5. var d = document.theForm.theDate.value;
  6. var y = d.substr(d.lastIndexOf("/")+1,d.length-1);
  7.     if(y >= 1930 && y <= 2003){
  8.     return true; // submit the form
  9.     }
  10. alert('Please enter a year between 1930 and 2003');
  11. document.theForm.theDate.value=d.substr(0,d.lastIndexOf("/")+1);
  12. document.theForm.theDate.focus();
  13. return false;
  14. }
  15. </script>
  16. <script type='text/javascript' src='dFilter.js'></script>
  17. </head>
  18. <body>
  19. <form name="theForm" action="" method="" onsubmit="return checkYear();">
  20. <table>
  21. <tr><td>Date:</td>
  22. <td><input value="" type="text" name="theDate" size="15" onKeyDown="javascript:return dFilter (event.keyCode, this, '##/##/####');"></td>
  23. </tr>
  24. </table>
  25. <input type="submit"><input type="reset">
  26. </form>
  27. </body>
  28. </html>


for a further description of how to use the mask see:
http://javascript.internet.com/forms/dfilter.html

The script does have one drawback in that it will only read numbers from the top row of the keyboard and not from the number pad.

A much simpler way to do this is to use three text boxes, one for each value:

Code: [ Select ]
<input type="text" name="month" maxlength="2" size="2"> /
<input type="text" name="day" maxlength="2" size="2"> /
<input type="text" name="year" maxlength="4" size="4">
  1. <input type="text" name="month" maxlength="2" size="2"> /
  2. <input type="text" name="day" maxlength="2" size="2"> /
  3. <input type="text" name="year" maxlength="4" size="4">


and then to check the year with the simpler method:

Code: [ Select ]
function checkYear(){
var d = document.theForm.year.value;
  if(d >= 1930 && d <= 2003){
  return true; // submit the form
  }
alert('Please enter a year between 1930 and 2003');
document.theForm.year.value="";
document.theForm.year.focus();
return false;
}
  1. function checkYear(){
  2. var d = document.theForm.year.value;
  3.   if(d >= 1930 && d <= 2003){
  4.   return true; // submit the form
  5.   }
  6. alert('Please enter a year between 1930 and 2003');
  7. document.theForm.year.value="";
  8. document.theForm.year.focus();
  9. return false;
  10. }
  • vickriz
  • Novice
  • Novice
  • User avatar
  • Posts: 31
  • Loc: Phlippines

Post 3+ Months Ago

RichB wrote:
The script does have one drawback in that it will only read numbers from the top row of the keyboard and not from the number pad.

A much simpler way to do this is to use three text boxes, one for each value:

Code: [ Select ]
<input type="text" name="month" maxlength="2" size="2"> /
<input type="text" name="day" maxlength="2" size="2"> /
<input type="text" name="year" maxlength="4" size="4">
  1. <input type="text" name="month" maxlength="2" size="2"> /
  2. <input type="text" name="day" maxlength="2" size="2"> /
  3. <input type="text" name="year" maxlength="4" size="4">


and then to check the year with the simpler method:

Code: [ Select ]
function checkYear(){
var d = document.theForm.year.value;
  if(d >= 1930 && d <= 2003){
  return true; // submit the form
  }
alert('Please enter a year between 1930 and 2003');
document.theForm.year.value="";
document.theForm.year.focus();
return false;
}
  1. function checkYear(){
  2. var d = document.theForm.year.value;
  3.   if(d >= 1930 && d <= 2003){
  4.   return true; // submit the form
  5.   }
  6. alert('Please enter a year between 1930 and 2003');
  7. document.theForm.year.value="";
  8. document.theForm.year.focus();
  9. return false;
  10. }


id like to used this three textbox. but what if after an input with each box how will the cursor jump focus automatic to next textbox. that would be 2digits for 1st txtbox then automatic focus to 2nd txtbox and on...

any idea how that it works?? thanks in advance RichB...
  • RichB
  • Guru
  • Guru
  • User avatar
  • Posts: 1121
  • Loc: Boston

Post 3+ Months Ago

this should do it:

Code: [ Select ]
<input type="text" name="month" maxlength="2" size="2" onkeyup="if(this.value.length>=2)document.theForm.day.focus()"> /
<input type="text" name="day" maxlength="2" size="2" onkeyup="if(this.value.length>=2)document.theForm.year.focus()"> /
<input type="text" name="year" maxlength="4" size="4">
  1. <input type="text" name="month" maxlength="2" size="2" onkeyup="if(this.value.length>=2)document.theForm.day.focus()"> /
  2. <input type="text" name="day" maxlength="2" size="2" onkeyup="if(this.value.length>=2)document.theForm.year.focus()"> /
  3. <input type="text" name="year" maxlength="4" size="4">
  • RichB
  • Guru
  • Guru
  • User avatar
  • Posts: 1121
  • Loc: Boston

Post 3+ Months Ago

Actually, you might as well validate all 3 fields and set a hidden field with the entire mm/dd/yyyy value for convenience sake. You can also add any missing leading 0's (if the user enters 1 for january, change it to 01 automatically), as well as auto-tabbing to the next field when they enter it correctly. I patched this script together from some old calendar functions that I had hanging around and tested it in IE6, NE7, Opera 7 and NE4.08 and it seemed to work in all of them. Use it at your own risk, but let me know if you have problems with it.

Code: [ Select ]
<html>
<head>
<script language="javascript">
<!--
var mm;
var dd;
var yyyy;
var daysInMonth;
var minYear = 1930; //inclusive, 1930 is the lowest valid year
var maxYear = 2003; //inclusive, 2003 is the highest valid year

function checkDate(){
    mm = document.theForm.month.value;
    dd = document.theForm.day.value;
    yyyy = document.theForm.year.value;
    setDaysInMonth(mm);     
    var submitting = (checkMonth() && checkDay() && checkYear());
    if(submitting){
        //set the hidden field with the whole date string
        document.theForm.mmddyyyy.value=mm+"/"+dd+"/"+yyyy;
    }
    return submitting;
}
function checkMonth(){
    if(mm!="" && mm >=1 && mm <=12){
        return true;
    }
    alert('valid months are 1 to 12');
    document.theForm.month.value="";
    document.theForm.month.focus();
    return false;
}
function checkDay(){
    if(dd!="" && dd >=1 && dd <= daysInMonth){
        return true;
    }
    alert('valid days are 1 to ' + daysInMonth);
    document.theForm.day.value="";
    document.theForm.day.focus();
    return false;
}
function checkYear(){
    if(yyyy.length==4 && yyyy >= minYear && yyyy <= maxYear){
        return true;
    }
    alert('Please enter a 4-digit year between '+minYear+' and '+maxYear);
    document.theForm.year.value="";
    document.theForm.year.focus();
    return false;
}
function setDaysInMonth(month){
    leap = (yyyy%400==0) || ((yyyy%4==0) && (yyyy%100 !=0));
    if(month==4 || month==6 || month==9 || month==11){
        daysInMonth = 30;
    }
    else if(month==2 && leap){
        daysInMonth = 29;
    }
    else if(month==2 && !leap){
        daysInMonth = 28;
    }
    else{
        daysInMonth = 31;
    }
}
//-->
</script>
</head>
<body onload="document.theForm.month.focus()">
<form name="theForm" action="" method="" onsubmit="return checkDate()">
<table>
<tr><td>Date:</td>
<td><input type="text" name="month" maxlength="2" size="2" onkeyup="if(this.value.length>=2)document.theForm.day.focus()" onblur="if(this.value.length==1)this.value='0'+this.value"> /
<input type="text" name="day" maxlength="2" size="2" onkeyup="if(this.value.length>=2)document.theForm.year.focus()" onblur="if(this.value.length==1)this.value='0'+this.value"> /
<input type="text" name="year" maxlength="4" size="4"> (mm/dd/yyyy)
</td>
</tr>
</table>
<input type="hidden" name="mmddyyyy" value="">
<input type="submit"><input type="reset">
</form>
</body>
</html>
  1. <html>
  2. <head>
  3. <script language="javascript">
  4. <!--
  5. var mm;
  6. var dd;
  7. var yyyy;
  8. var daysInMonth;
  9. var minYear = 1930; //inclusive, 1930 is the lowest valid year
  10. var maxYear = 2003; //inclusive, 2003 is the highest valid year
  11. function checkDate(){
  12.     mm = document.theForm.month.value;
  13.     dd = document.theForm.day.value;
  14.     yyyy = document.theForm.year.value;
  15.     setDaysInMonth(mm);     
  16.     var submitting = (checkMonth() && checkDay() && checkYear());
  17.     if(submitting){
  18.         //set the hidden field with the whole date string
  19.         document.theForm.mmddyyyy.value=mm+"/"+dd+"/"+yyyy;
  20.     }
  21.     return submitting;
  22. }
  23. function checkMonth(){
  24.     if(mm!="" && mm >=1 && mm <=12){
  25.         return true;
  26.     }
  27.     alert('valid months are 1 to 12');
  28.     document.theForm.month.value="";
  29.     document.theForm.month.focus();
  30.     return false;
  31. }
  32. function checkDay(){
  33.     if(dd!="" && dd >=1 && dd <= daysInMonth){
  34.         return true;
  35.     }
  36.     alert('valid days are 1 to ' + daysInMonth);
  37.     document.theForm.day.value="";
  38.     document.theForm.day.focus();
  39.     return false;
  40. }
  41. function checkYear(){
  42.     if(yyyy.length==4 && yyyy >= minYear && yyyy <= maxYear){
  43.         return true;
  44.     }
  45.     alert('Please enter a 4-digit year between '+minYear+' and '+maxYear);
  46.     document.theForm.year.value="";
  47.     document.theForm.year.focus();
  48.     return false;
  49. }
  50. function setDaysInMonth(month){
  51.     leap = (yyyy%400==0) || ((yyyy%4==0) && (yyyy%100 !=0));
  52.     if(month==4 || month==6 || month==9 || month==11){
  53.         daysInMonth = 30;
  54.     }
  55.     else if(month==2 && leap){
  56.         daysInMonth = 29;
  57.     }
  58.     else if(month==2 && !leap){
  59.         daysInMonth = 28;
  60.     }
  61.     else{
  62.         daysInMonth = 31;
  63.     }
  64. }
  65. //-->
  66. </script>
  67. </head>
  68. <body onload="document.theForm.month.focus()">
  69. <form name="theForm" action="" method="" onsubmit="return checkDate()">
  70. <table>
  71. <tr><td>Date:</td>
  72. <td><input type="text" name="month" maxlength="2" size="2" onkeyup="if(this.value.length>=2)document.theForm.day.focus()" onblur="if(this.value.length==1)this.value='0'+this.value"> /
  73. <input type="text" name="day" maxlength="2" size="2" onkeyup="if(this.value.length>=2)document.theForm.year.focus()" onblur="if(this.value.length==1)this.value='0'+this.value"> /
  74. <input type="text" name="year" maxlength="4" size="4"> (mm/dd/yyyy)
  75. </td>
  76. </tr>
  77. </table>
  78. <input type="hidden" name="mmddyyyy" value="">
  79. <input type="submit"><input type="reset">
  80. </form>
  81. </body>
  82. </html>


If the user enters a date like January 5, 2000 - either as 1/5/2000 or 01/05/2000 this form should submit these values:

month = 01
day = 05
year = 2000
mmddyyyy = 01/05/2000

Then you can use the values individually or just use the whole date.
  • vickriz
  • Novice
  • Novice
  • User avatar
  • Posts: 31
  • Loc: Phlippines

Post 3+ Months Ago

Wow fantastic! thats really greate script u made..

thanks alot for your patient and giving me your time to help..

you save me my day bro...
  • helder.meneses
  • Born
  • Born
  • helder.meneses
  • Posts: 1

Post 3+ Months Ago

hi there...

i loved the dfilter script, but i had a small problem about the keypad numbers. i have resolved that width this:

replace this in the dfilter function :
Code: [ Select ]
else if ( ((key>47&&key<58)||(key>95&&key<106)) && dFilterNum.length<dFilterMax(dFilterMask) )
        {
        if(key>95&&key<106){key-=48;}
    dFilterNum=dFilterNum+String.fromCharCode(key);
        }

        var dFilterFinal='';
  1. else if ( ((key>47&&key<58)||(key>95&&key<106)) && dFilterNum.length<dFilterMax(dFilterMask) )
  2.         {
  3.         if(key>95&&key<106){key-=48;}
  4.     dFilterNum=dFilterNum+String.fromCharCode(key);
  5.         }
  6.         var dFilterFinal='';

the only thing that is diferent is the line if(key>95&&key<106){key-=48;} where i subtract 48.


now it is perfect....
  • plafond
  • Born
  • Born
  • plafond
  • Posts: 1

Post 3+ Months Ago

Nice script. Usefull as a number mask but I still prefer a calender script.

I have one improvement for this script. It stops the masks charaters from trailing at the end.

Example: For a semi complete date with mask ####-##-## the text box will show 1990-1 instead of 1990-1- with the cursor after the last dash.

lnies 64 to 70 now look like this
Code: [ Select ]
else if (dFilterMask.charAt(dFilterStep)!='#')
{
 // Add mask characters only if there still numbers to add (no trailing mask characters)
 if (dFilterNum.length!=0){
  dFilterFinal = dFilterFinal + dFilterMask.charAt(dFilterStep);     
 }
}
  1. else if (dFilterMask.charAt(dFilterStep)!='#')
  2. {
  3.  // Add mask characters only if there still numbers to add (no trailing mask characters)
  4.  if (dFilterNum.length!=0){
  5.   dFilterFinal = dFilterFinal + dFilterMask.charAt(dFilterStep);     
  6.  }
  7. }
  • alexmos
  • Beginner
  • Beginner
  • User avatar
  • Posts: 48
  • Loc: Europe, RO

Post 3+ Months Ago

I was wondering how I could do that... :idea:
  • __celso
  • Born
  • Born
  • __celso
  • Posts: 1

Post 3+ Months Ago

This script is great. However I'm having a trouble with IE. I just added an "onChange" event in my input field and it works great with FireFox but not with IE. For some reason IE doesn't fire my "onChange" event. Does anyone know how can I fix that?
  • UberGeek
  • Born
  • Born
  • User avatar
  • Posts: 4

Post 3+ Months Ago

I like the dFilter script and use it now on my site, but I did find a problem with Netscape 7.2 doubling the keys until I changed lines 30-32 with this

Code: [ Select ]
function dFilter (e, textbox, dFilterMask)
{        
    var key = window.event ? e.keyCode : e.which;


-=-=-=-=-=-=-=-=-=-=-=-

Also had to change the calling statment to:


onKeyUp="dFilter(event,this,'##/##/#### ##:##')"
  1. function dFilter (e, textbox, dFilterMask)
  2. {        
  3.     var key = window.event ? e.keyCode : e.which;
  4. -=-=-=-=-=-=-=-=-=-=-=-
  5. Also had to change the calling statment to:
  6. onKeyUp="dFilter(event,this,'##/##/#### ##:##')"



But to provide a date and time check I wrote the following Script for the mask ##/##/#### ##:##

Code: [ Select ]
if (Value.length==16) {
 var month = Value.substring(0,2);
 var day= Value.substring(3,5);
 var year = Value.substring(6,10);
 var hour = Value.substring(11,13);
 var minute = Value.substring(14,16);
 var dateerror = "The date you entered did not meet the following requirement(s) :\n\n";
 var datetest = true;
 var AMPM ='';
 var today = new Date();
 if ((today.getYear()-year)>1) {
   datetest = false;
   dateerror = dateerror + "Must have the current year\n";
 }else if ((year-today.getYear())>0) {
   datetest = false;
   dateerror = dateerror + "Can not enter year in the future\n";
 }else if((today.getYear()-year)>0){
  if (!(month==12 && (today.getMonth()==1) && ((today.getYear()-1)==year))) {
   datetest = false;
   dateerror = dateerror + "Can not use last year\n";
  }
 }
 if (month>12) {
  datetest = false;
  dateerror = dateerror + "Must be an two digit month\n";
 } else {
  if (month > (today.getMonth()+1)) {
   datetest = false;
   dateerror = dateerror + "Can not enter month in the future\n";
  } else if (((today.getMonth()+1) - month) >1){
   datetest = false;
   dateerror = dateerror + "Can not enter prior to last month\n";
  } else {
   if (month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12 ) {
    if (day>31){
     dateerror = dateerror + "Must be an two digit day between 01 and 31\n";
     datetest = false;
    }
   } else if(month==2) {
    if (Leap(year)) {
     if (day>29){
      dateerror = dateerror + "Must be an two digit day between 01 and 29\n";
      datetest = false;
     }
    } else {
     if (day>28){
      dateerror = dateerror + "Must be an two digit day between 01 and 28\n";
      datetest = false;
     }
    }     
   } else {
    if (day>30){
     dateerror = dateerror + "Must be an two digit day between 01 and 30\n";
     datetest = false;
    }
   }
  }
 }
 if (day<1) {
  dateerror = dateerror + "Must be an two digit day greater then 0\n";
  datetest = false;
 }
 if ((day>(today.getDate+1)) && (month==(today.getMonth+1))) {
  dateerror = dateerror + "Can not enter day in the future\n";
  datetest = false;
 }
 if ((day<(today.getDate+1)) && (month==(today.getMonth))) {
  dateerror = dateerror + "Can not enter day over 30 days in the past\n";
  datetest = false;
 }
 if (hour>12 && hour<25) {
   hour = (hour-12);
   AMPM='PM';
 }
 if (hour ==0) {
   hour=(hour+12);
   AMPM='AM';
 }
 if (hour >24) {
  dateerror = dateerror + "Hour must be in 12 or 24 hour format only\n";
  datetest = false;
 }
 if (minute >59) {
  dateerror = dateerror + "Minute must be less then 60\n";
  datetest = false;
 }
 if (datetest) {
  while (!(AMPM=='AM' || AMPM=='PM')) {
   AMPM=prompt('is this AM or PM?',AMPM);
  }
  NewValue=month+"/"+day+"/"+year+" "+hour+":"+minute+" "+AMPM;
  field.value=NewValue;
  field.style.backgroundColor='#FFFFFF';
  
 } else {
  alert(dateerror);
  field.style.backgroundColor='#FFFF00';
  field.focus();
  
 }
} else if (Value.length==19) {
 
 
} else {
 alert('Please completely fill out this field');
 field.style.backgroundColor='#FFFF00';
 field.focus();
}
  1. if (Value.length==16) {
  2.  var month = Value.substring(0,2);
  3.  var day= Value.substring(3,5);
  4.  var year = Value.substring(6,10);
  5.  var hour = Value.substring(11,13);
  6.  var minute = Value.substring(14,16);
  7.  var dateerror = "The date you entered did not meet the following requirement(s) :\n\n";
  8.  var datetest = true;
  9.  var AMPM ='';
  10.  var today = new Date();
  11.  if ((today.getYear()-year)>1) {
  12.    datetest = false;
  13.    dateerror = dateerror + "Must have the current year\n";
  14.  }else if ((year-today.getYear())>0) {
  15.    datetest = false;
  16.    dateerror = dateerror + "Can not enter year in the future\n";
  17.  }else if((today.getYear()-year)>0){
  18.   if (!(month==12 && (today.getMonth()==1) && ((today.getYear()-1)==year))) {
  19.    datetest = false;
  20.    dateerror = dateerror + "Can not use last year\n";
  21.   }
  22.  }
  23.  if (month>12) {
  24.   datetest = false;
  25.   dateerror = dateerror + "Must be an two digit month\n";
  26.  } else {
  27.   if (month > (today.getMonth()+1)) {
  28.    datetest = false;
  29.    dateerror = dateerror + "Can not enter month in the future\n";
  30.   } else if (((today.getMonth()+1) - month) >1){
  31.    datetest = false;
  32.    dateerror = dateerror + "Can not enter prior to last month\n";
  33.   } else {
  34.    if (month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12 ) {
  35.     if (day>31){
  36.      dateerror = dateerror + "Must be an two digit day between 01 and 31\n";
  37.      datetest = false;
  38.     }
  39.    } else if(month==2) {
  40.     if (Leap(year)) {
  41.      if (day>29){
  42.       dateerror = dateerror + "Must be an two digit day between 01 and 29\n";
  43.       datetest = false;
  44.      }
  45.     } else {
  46.      if (day>28){
  47.       dateerror = dateerror + "Must be an two digit day between 01 and 28\n";
  48.       datetest = false;
  49.      }
  50.     }     
  51.    } else {
  52.     if (day>30){
  53.      dateerror = dateerror + "Must be an two digit day between 01 and 30\n";
  54.      datetest = false;
  55.     }
  56.    }
  57.   }
  58.  }
  59.  if (day<1) {
  60.   dateerror = dateerror + "Must be an two digit day greater then 0\n";
  61.   datetest = false;
  62.  }
  63.  if ((day>(today.getDate+1)) && (month==(today.getMonth+1))) {
  64.   dateerror = dateerror + "Can not enter day in the future\n";
  65.   datetest = false;
  66.  }
  67.  if ((day<(today.getDate+1)) && (month==(today.getMonth))) {
  68.   dateerror = dateerror + "Can not enter day over 30 days in the past\n";
  69.   datetest = false;
  70.  }
  71.  if (hour>12 && hour<25) {
  72.    hour = (hour-12);
  73.    AMPM='PM';
  74.  }
  75.  if (hour ==0) {
  76.    hour=(hour+12);
  77.    AMPM='AM';
  78.  }
  79.  if (hour >24) {
  80.   dateerror = dateerror + "Hour must be in 12 or 24 hour format only\n";
  81.   datetest = false;
  82.  }
  83.  if (minute >59) {
  84.   dateerror = dateerror + "Minute must be less then 60\n";
  85.   datetest = false;
  86.  }
  87.  if (datetest) {
  88.   while (!(AMPM=='AM' || AMPM=='PM')) {
  89.    AMPM=prompt('is this AM or PM?',AMPM);
  90.   }
  91.   NewValue=month+"/"+day+"/"+year+" "+hour+":"+minute+" "+AMPM;
  92.   field.value=NewValue;
  93.   field.style.backgroundColor='#FFFFFF';
  94.   
  95.  } else {
  96.   alert(dateerror);
  97.   field.style.backgroundColor='#FFFF00';
  98.   field.focus();
  99.   
  100.  }
  101. } else if (Value.length==19) {
  102.  
  103.  
  104. } else {
  105.  alert('Please completely fill out this field');
  106.  field.style.backgroundColor='#FFFF00';
  107.  field.focus();
  108. }
  • UberGeek
  • Born
  • Born
  • User avatar
  • Posts: 4

Post 3+ Months Ago

OK so I gave you all that great code and forgot to mention that you will need the Leap Year Function

Here it is.

Code: [ Select ]


//This version has been modified by the UberGeek

//Original Script can be found at http://www.dataip.co.uk/Reference/Leap.js


function Leap(Year)    {
    if ( (Year % 4) == 0){
        // It is exactly divisible by 4

        if ( (Year % 100) == 0)    {
            // It is exactly divisible by 100
            // Is it also exactly divisible by 400?
            Result = ( (Year % 400) == 0);
        } else    {
            return true;
        }
    } else {
        // It is not exactly divisible by 4
        // It is not a leap year
        return false;
    }
}
  1. //This version has been modified by the UberGeek
  2. //Original Script can be found at http://www.dataip.co.uk/Reference/Leap.js
  3. function Leap(Year)    {
  4.     if ( (Year % 4) == 0){
  5.         // It is exactly divisible by 4
  6.         if ( (Year % 100) == 0)    {
  7.             // It is exactly divisible by 100
  8.             // Is it also exactly divisible by 400?
  9.             Result = ( (Year % 400) == 0);
  10.         } else    {
  11.             return true;
  12.         }
  13.     } else {
  14.         // It is not exactly divisible by 4
  15.         // It is not a leap year
  16.         return false;
  17.     }
  18. }
  • epe1
  • Born
  • Born
  • epe1
  • Posts: 1

Post 3+ Months Ago

Hi guys, I'm new to this forum, and I found the calendar script very useful! Problem is that I have to deal with with up to 5 different dates in one script. Any ideas on how I can declare variables for different dates?

<html>
<head>
<script language="javascript">
<!--
var mm; // THIS IS WHERE MY PROBLEM IS!
var dd;
var yyyy;
var daysInMonth;
var minYear = 1980; //inclusive, 1980 is the lowest valid year
var maxYear = 2020; //inclusive, 2020 is the highest valid year

Post Information

  • Total Posts in this topic: 15 posts
  • Users browsing this forum: Bogey and 128 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.