Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

time function RegExps

Status
Not open for further replies.

jwoods7

Technical User
Feb 19, 2003
36
US
Wow, I've started to research RegExps for data verification...not a small topic.

Anyway, my sample script isn't working and I don't know why, can anyone help?

For this one, I'm looking to test whether a string is a time (ie; 11:45 am)

Why does this alert "false"?

function testField(fieldname){
var fieldval
fieldval = "12:36 pm"

var re = new RegExp( fieldval, '^([1-12]):))([1-60])$' );

alert(re.test(fieldval))
}
 
How about
Code:
'^[0-9]|[10-2]:[0-5][0-9] [ap]m$'

--Chessbot

"In that blessed region of Four Dimensions, shall we linger on the threshold of the Fifth, and not enter therein? Ah, no! [...] Then, yielding to our intellectual onset, the gates of the Sixth Dimension shall fly open; after that a Seventh, and then an Eighth -- --" Flatland, A. Square (E. A. Abbott)
 
The part of your expression [10-2] will not work - it will be treated as "one or zero to two". The character class ([]) only supports SINGLE characters. what you want is:
Code:
"^(0?[1-9])|(1[0-2]):[0-5][0-9] [ap]m$"
If the time is required to have a leading zero in the hours, take out the ?

Meddle not in the affairs of dragons,
for you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top