Solved

Auto format date and time form input

Posted on 2004-08-05
5
563 Views
Last Modified: 2012-06-21
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
Comment
Question by:misterys
  • 2
  • 2
5 Comments
 
LVL 14

Expert Comment

by:dfu23
ID: 11732559
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
 

Author Comment

by:misterys
ID: 11735328
Thanks for the info dfu23, but i'd like to get this script to work...
0
 
LVL 63

Accepted Solution

by:
Zvonko earned 100 total points
ID: 11736916
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
 

Author Comment

by:misterys
ID: 11737360
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
 
LVL 63

Expert Comment

by:Zvonko
ID: 11737833
:-)
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now