Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Timestamp validation

Posted on 2004-03-23
5
Medium Priority
?
1,598 Views
Last Modified: 2012-05-04
HI,

  I have Timestamp in mm-dd-yyyy HH:MI:ss  format, which will be entered in TextBox.
I wanted to validate it on form submit.

I want Timestamp validation code please...!!!

Thanks
0
Comment
Question by:mvkraju
  • 2
  • 2
5 Comments
 
LVL 16

Expert Comment

by:jaysolomon
ID: 10661633
do you just want to make sure it is in the correct format? or validate a specific time and date?
0
 

Author Comment

by:mvkraju
ID: 10662117
just it's in the correct format.
I mean say 'mm' should be between 1-12 only and 'dd' sould be based on month.
HH should be 0-23 .. etc.
0
 
LVL 12

Accepted Solution

by:
lil_puffball earned 200 total points
ID: 10662302
Here's a script:

<script>
var mdays=new Array(31,28,31,30,31,30,31,31,30,31,30,31);

function valid(time){
time=time.split(' ');
if(time.length!=2){return false;}
time[0]=time[0].split('-');
if(time[0].length!=3){return false;}
time[1]=time[1].split(':');
if(time[1].length!=3){return false;}
//validate month
if(isNaN(time[0][0]*1)||time[0][0]<1||time[0][0]>12){return false;}
//validate year
if(isNaN(time[0][2]*1)){return false;}
//validate hours
if(isNaN(time[1][0]*1)||time[1][0]<0||time[1][0]>23){return false;}
//validate minutes
if(isNaN(time[1][1]*1)||time[1][1]<0||time[1][1]>59){return false;}
//validate seconds
if(isNaN(time[1][2]*1)||time[1][2]<0||time[1][2]>59){return false;}
//validate days
if(isNaN(time[0][1]*1)||time[0][1]<1){return false;}
if(time[0][1]!=2){
  if(time[0][1]>mdays[time[0][1]-1]){return false;}
}else{
  if(leapYr(time[0][2])){if(time[0][1]>mdays[1]+1){return false;}}
  else{if(time[0][1]>mdays[1]){return false;}}
}
//else return true
return true;
}

function leapYr(yr){
if(yr%400==0 || (yr%100!=0&&yr%4==0)){return true;}
return false;
}
</script>

<form>
<input type=text name=time>
<input type=button onclick="if(valid(this.form.time.value)){alert('Valid!');}else{alert('Invalid.');}" value=Go>
</form>
0
 

Author Comment

by:mvkraju
ID: 10667281
good job. excellent!
0
 
LVL 12

Expert Comment

by:lil_puffball
ID: 10671789
Thanks for the points and the A. :)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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'…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…
Suggested Courses

782 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