Solved

Syntax for a regular_expression validation

Posted on 2003-10-22
10
172 Views
Last Modified: 2013-12-24
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?
0
Comment
Question by:SherryG
  • 5
  • 4
10 Comments
 
LVL 16

Expert Comment

by:_nn_
ID: 9600013
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
0
 

Author Comment

by:SherryG
ID: 9600819
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.
0
 

Author Comment

by:SherryG
ID: 9600820
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.
0
 

Author Comment

by:SherryG
ID: 9600821
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.
0
 
LVL 2

Expert Comment

by:jonnygo55
ID: 9601728
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...
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 16

Accepted Solution

by:
_nn_ earned 50 total points
ID: 9605555
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
0
 

Author Comment

by:SherryG
ID: 9606823
Thanks HTH.  There was a problem on my page.  Once I resolved it your code worked perfectly.
0
 
LVL 16

Expert Comment

by:_nn_
ID: 9606900
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" :)
0
 

Author Comment

by:SherryG
ID: 9606911
Sorry about that...I just assumed they were your initials!
0
 
LVL 16

Expert Comment

by:_nn_
ID: 9606936
>> 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 :)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Most ColdFusion developers get confused between the CFSet, Duplicate, and Structcopy methods of copying a Structure, especially which one to use when. This Article will explain the differences in the approaches with examples; therefore, after readin…
One of the typical problems I have experienced is when you have to move a web server from one hosting site to another. You normally prepare all on the new host, transfer the site, change DNS and cross your fingers hoping all will be ok on new server…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

706 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now