Reply
Time and date format regular expression
Old 09-08-2006, 09:35 AM Time and date format regular expression
Average Talker

Posts: 16
Hi,

I am going to input a long string,which prompts a user for a time and date in the format
Code:
  hh:mm mm/dd/yyyy
It is valid only two digit for hour, month, date.
It is invalid to use one digit for hour such as 1:30 10/03/2005
Code:
    str= prompt("Enter the time and date in the format\n" + "hh:mm mm/dd/yyyy",
                                                                                              "00:00 01/01/2000");
how to write the regular expression.

Thank in advance!

zhshqzyc
zhshqzyc is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
     
Old 09-08-2006, 08:30 PM Re: Time and date format regular expression
funkdaddu's Avatar
Web Design Snob

Posts: 636
This should work:
Code:
var str = prompt("Enter the time and date in the format\n" + "hh:mm mm/dd/yyyy","00:00 01/01/2000");

if (str.match(/^\d\d:\d\d[ ]\d\d\/\d\d\/\d{4}$/) != null) {
	alert("Format Correct");
} else {
	alert("Invalid Format");
}
funkdaddu is offline
Reply With Quote
View Public Profile Visit funkdaddu's homepage!
 
Old 09-09-2006, 09:08 AM Re: Time and date format regular expression
Average Talker

Posts: 16
thanks funkdaddu!

if the format is correct, how to output string?

I mean
Code:
document.writeln("please supply missing fragment");
zhshqzyc is offline
Reply With Quote
View Public Profile
 
Old 09-09-2006, 12:28 PM Re: Time and date format regular expression
funkdaddu's Avatar
Web Design Snob

Posts: 636
The string is str:

document.writeln(str);
__________________
Will Work For Talkputation...
funkdaddu is offline
Reply With Quote
View Public Profile Visit funkdaddu's homepage!
 
Old 09-09-2006, 02:49 PM Re: Time and date format regular expression
Average Talker

Posts: 16
yes, however



str is str = prompt( "Enter the time and date in the format\n" +
"hh:mm mm/dd/yyyy",
"00:00 01/01/2000" );
how can I only to display the time and data part?

have a good day!
zhshqzyc is offline
Reply With Quote
View Public Profile
 
Old 09-09-2006, 10:51 PM Re: Time and date format regular expression
funkdaddu's Avatar
Web Design Snob

Posts: 636
You can spilt the str string on the space into an array:

str.split(" ")[0]; //will give you the time
str.split(" ")[1]; //will give you the date
__________________
Will Work For Talkputation...
funkdaddu is offline
Reply With Quote
View Public Profile Visit funkdaddu's homepage!
 
Old 09-10-2006, 08:50 PM Re: Time and date format regular expression
Average Talker

Posts: 16
Hi, your answer is pretty great. But what is wrong with my expression?

Code:
reg = /^\d{2}:\d{2} \d{2}\/\\d{2}\/\\d{4}$/
I am a novice. I spent a lot time, still could not figure out,

the one more question is how to seperate the string into five part.

such as

RegExp.$1 = ...// hour
RegExp.$2 = ...// minute
RegExp.$3 = ...// month
RegExp.$4 = ...// day
RegExp.$5 = ...// year

It needs to remove the special characters such ":","/".

Thanks!
zhshqzyc is offline
Reply With Quote
View Public Profile
 
Old 09-10-2006, 11:18 PM Re: Time and date format regular expression
funkdaddu's Avatar
Web Design Snob

Posts: 636
You just need to put each bit you want in between parentheses, and you don't need the double //:

Code:
reg = /^(\d{2}):(\d{2}) (\d{2})\/(\d{2})\/(\d{4})$/
then you can use the RegExp.$1 for the hour, RegExp.$2 for the minutes, RegExp.$3 for the Month... etc...
__________________
Will Work For Talkputation...
funkdaddu is offline
Reply With Quote
View Public Profile Visit funkdaddu's homepage!
 
Old 09-11-2006, 07:58 AM Re: Time and date format regular expression
Average Talker

Posts: 16
I appreciate your assistance.

God bless you!
zhshqzyc is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Time and date format regular expression
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.16735 seconds with 13 queries