Time Range Validation and DateTime Control Regular Expressions

  • 12 August 2021
  • 3 replies
  • 262 views

I'm working on a form where I need to check if the user has entered a time outside of a certain range (as an example, let's use 12:00 PM - 10:00 PM). I have this sort of working using invalidate rules but the validation doesn't update until the user either selects/enters a date or tries to submit the form. So if a user changes the time from 12:00 AM to 3:00 PM, the form would still say that the time was out of range until the user changes the date or clicks save/submit. Is this normal?

 

In trying to deal with this, I thought maybe a regular expression validation on the control would be more effective/responsive, but I haven't been able to figure out a valid regular expression. Does anyone know what format the date is in when comparing to a regular expression from the DateTime control's "Use a regular expression" validation option? I've tried the format MM/dd/yyyy hh:mm:ss tt (11/28/2021 10:30:00 AM), but it doesn't seem to work even without trying to check for specific values in hours/minutes.


3 replies

I forgot to mention, I'm working in responsive forms, not classic.

Userlevel 1
Badge +8

Add a calculated control value to the form, with the formula: 



 


if(and(not(isNullorEmpty(Date)),or(greaterThanOrEqual(formatDate(Date,"HH"),22),lessThanOrEqual(formatDate(Date,"HH"),12))),"Please select a time between noon and 10PM","")


where Date is your column selected from the Named Controls tab. 


This will not show a message until they have selected a date. 


 


If you want the message to appear before they even select a date, then use the simpler formula:


if(or(greaterThanOrEqual(formatDate(Date,"HH"),22),lessThanOrEqual(formatDate(Date,"HH"),12)),"Please select a time between noon and 10PM","") , again using the Named Control for your date column in place of Date.


 


Of course, you could use this formula as an invalidate rule, but if you want them to see in real time, the calculated value works when they change the value.


 


If you want a bold red message, simply add a Label control with the message in bold red text, and use the following formula as a rule to hide the label:


not(and(not(isNullorEmpty(Date)),or(greaterThanOrEqual(formatDate(Date,"HH"),22),lessThanOrEqual(formatDate(Date,"HH"),12)))) where Date is the Named Control of your field.


 


Adjust the greaterThanOrEqual to greaterThan and lessThanOrEqual to lessThan as appropriate for your conditions.


 


Best of luck!


Thanks so much for your help -- I'm still having some issues getting the formatting to work, but this has helped a lot. 🙂 I'm working with validation rules as well so the form wouldn't submit with invalid dates, but this technique allows me to hide the messages from validation rules too. Thanks again and have a good one

Reply