Syntax for a regular_expression validation

SherryG
SherryG used Ask the Experts™
on
I need to validate a text field with a regular_expression.  I want to ensure that a user enters a time in the format HH:MM AM or HH:MM PM.

I thought I had the syntax correct...but it is not working correctly.

<cfinput type="text" validate="regular_expression" pattern="[0-9]{1,2}\:[0-9]{2}\s[AM|PM]" name="starttime" required="yes" message="Please enter a valid start time in the format HH:MM AM or HH:MM PM.">

Any suggestions?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Wild guesses (I don't know CF, but I know regexps)

- ":" doesn't need to be escaped
- "(|)" instead of "[|]"

pattern="[0-9]{1,2}:[0-9]{2}\s(AM|PM|am|pm)"

Possibly, you'd like to prevent anything else in the expression

pattern="^[0-9]{1,2}:[0-9]{2}\s(AM|PM|am|pm)$"

HTH

Author

Commented:
That partially works.

If I enter 1:00 I get the error indicating that I need to use the correct format, in which case I change it to 1:00 PM and it works fine.

If I enter 11:00 it goes directly into the DB without an error.  I need the user to be prompted to enter the AM or PM if they forget.  For some reason, the code is not catching that error.

Any suggestions.

Author

Commented:
That partially works.

If I enter 1:00 I get the error indicating that I need to use the correct format, in which case I change it to 1:00 PM and it works fine.

If I enter 11:00 it goes directly into the DB without an error.  I need the user to be prompted to enter the AM or PM if they forget.  For some reason, the code is not catching that error.

Any suggestions.
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

Author

Commented:
That partially works.

If I enter 1:00 I get the error indicating that I need to use the correct format, in which case I change it to 1:00 PM and it works fine.

If I enter 11:00 it goes directly into the DB without an error.  I need the user to be prompted to enter the AM or PM if they forget.  For some reason, the code is not catching that error.

Any suggestions.
ignore this if it not an option but why dont you create a little dropdown with AM and PM and add the 12 hours accordingly on the server side...
Commented:
Hmm, strange. I've just finished testing following one in javascript and it works ok for my IE6 :

pattern="^\d{1,2}:\d\d\s(AM|PM|am|pm)$"

HTH

Author

Commented:
Thanks HTH.  There was a problem on my page.  Once I resolved it your code worked perfectly.

Commented:
Thanks and glad I could help a it.

PS: For future reference, HTH is not my name, it's an acronym and stands for "Hope This Helps" or "Hope That Helps" :)

Author

Commented:
Sorry about that...I just assumed they were your initials!

Commented:
>> Sorry about that...I just assumed they were your initials!

Your assumption was perfectly normal and I should be the one apologizing for being a lazy typer :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial