• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 574
  • Last Modified:

Auto format date and time form input

Hello all!
Zvonko has previously provided this wicked script to auto format date input.  I am looking for help on the extra code to do the same for the time using 24hr clock 13:30.  I have tried to annotate the code but my poor js skills have earnt me frustration but no results.  I've got as far as the space ;-)=.  Any help and guidance gratefully received...  Sorry, but I won't be able to answer questions, if there are any, 'til the morning, as I'm off to bed!

function enterDate(theField){
  var dV = theField.value;
  if(dV.length==1 && dV>1) dV = "0"+dV;
  if(dV.length==2){
    if(dV<32) dV = dV+"/";
    else {
      alert("First position is the Day.")
      theField.select();
    }
  }
  if(dV.length==4){
    aV = dV.split("/");
    if(aV[1]>2) dV = aV[0]+"/0"+aV[1];
  }
  if(dV.length==5){
    aV = dV.split("/");
    if(aV[1]<13) dV = dV+"/200";
    else {  
      alert("Second position is the Month.")
      theField.select();
    }
      }
  if(dV.length==10){
    aV = dV.split("/");
    if(aV[2]<2004||aV[2]>2005){
      alert("Only years betwean 2004 and 2005 allowed.")
      }
  if (dV.length==10){
      dV = dV+" ";
      }
  if(dV.length==11){
      aV = dV.split(":");
            if(aV[1]>2) dV = aV[0]+" 0"+aV[1];
            }
  if(dV.length==13){
      aV = dV.split(":");
            if(aV[1]<24) dV = dV+" ";
                  else {  
                  alert("Use 24hr clock.")
                  theField.select();
                  }
      }
  if(dV.length==14){
      aV = dV.split(":");
            if(aV[1]>9) dV = aV[0]+"/0"+aV[1];
      }
  if(dV.length==13){
      aV = dV.split(":");
            if(aV[1]<60);
                  else {
                  alert("Use 24hr clock.")
                  theField.select();
                  }
      }
  if(dV!=theField.value) theField.value=dV;
}
0
misterys
Asked:
misterys
  • 2
  • 2
1 Solution
 
dfu23Commented:
even though you already have a script on hand I've gotta suggest the date formatting and validation script by matt kruse:

http://www.mattkruse.com/javascript/date/

you can easily output dates and time however you want using this script ... the format string for 24hr clock would be something like: HH:mm
0
 
misterysAuthor Commented:
Thanks for the info dfu23, but i'd like to get this script to work...
0
 
ZvonkoSystems architectCommented:
How about this:

<html>
<head>
<script>
function enterDate(theField){
  var fMsg = "Please use this Formet: MM/DD/YYYY hh:mm";
  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) vP = dV.match(/^(\d{1,2})\/(\d{1,2})\/(\d{1,4})\ (\d{1,2})\:?$/);
  if(!vP) vP = dV.match(/^(\d{1,2})\/(\d{1,2})\/(\d{1,4})\ (\d{1,2})\:(\d{1,2})$/);
  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);
    if(vP[4]>24) return chkMsg(theField,"Hours greater then 24\n"+fMsg);
    if(vP[5]>59) return chkMsg(theField,"Minutes greater then 59\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," ");
    dV += chkAdd(vP[4],2,2,":");
    dV += chkAdd(vP[5],2,5,"");
    if(dV!=theField.value) theField.value=dV;
  } 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="16" maxLength="16" onKeyUp="enterDate(this)">
</form>
</body>
</html>

0
 
misterysAuthor Commented:
Dude!  Thats gr8 - works brilliantly!  I'm just sad i couldn't annotate your original one to work with the time too.  All I managed to get working was the space after the date!!  Increased the points to 100 since you completely rewrote a new version.
kr harry
0
 
ZvonkoSystems architectCommented:
:-)
Thanks.

Oh, and the reason why I had to rewrite the code was that you can manage to check three number values for limits, but every next value doubles the cascade of checks if you do not split the work in sections and functions.

0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

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