Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Auto format date and time form input

Posted on 2004-08-05
5
Medium Priority
?
572 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 400 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
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…

670 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