Ravi
Ravi

Reputation: 31

How to set entry filter for 24 hour time format in the listbox column in 4d database

I have a listbox with 2 columns (say "from time" and "to time"). I want to filter user input by 24 hour format only i.e. no other key should be allowed to press except 24 hour time format (i.e 00:00 to 24:00) in the cell. I tried with this: &"0-2"#&"0-3"#:& "0-5"#&"0-9"# it works well but it didn't allow to put something 19:22 or the value after 13:59 in the cell value as I haven't passed other optional value for that 24 hour time format. In regex, it's a bit easy to achieve this ('/^([01][0-9]|2[0-3]):([0-5][0-9])$/') but not sure how this can be done in the 4d database listbox cell field.

Any help would be appreciated. Thanks.

Upvotes: 0

Views: 146

Answers (1)

kirkBrooks
kirkBrooks

Reputation: 91

I suggest you make the list box columns text and manage the display and UI yourself if you require fine control over the input values.

The Time string method will convert a time value for you:

$timeStr:=Time string(Current time) // $timeStr = "07:23:45"

The input filters are not RegEx and will not give you the kind of fine control you need. They will let you filter unwanted characters (anything besides numbers for instance). Try this for the Entry Filter on the column !0&9##:##:##

The result will be a text string of the numbers entered. Write a method to take the input string, parse the elements, validate them, update the data source and then return a properly formatted string for display. I would use the On data change form event as the trigger for running the method.

    $h:=Num(Substring($inputStr;1;2))
    $m:=Num(Substring($inputStr;3;2))
    $s:=Num(Substring($inputStr;5;2))

Recent versions of 4D manage time values as seconds since midnight which is why you may get a longint value instead of a 'time' value sometimes. And depending on which version you are using and the type of listbox (collection, entity selection, array, etc.) you may not even have a 'time' type option for the list box column which can be confusing. Given all that it's just easier to stringify the value and work with that.

For example, what do you want to do if a user enters "33:45:00"? If you want to reject the "33" at the outset you can do this by evaluating each character as it is typed. The On After Keystroke form event lets you run your evaluation method after each change in the field and the Get edited text command allows you to see what the user is entering. https://doc.4d.com/4Dv18R3/4D/18-R3/Get-edited-text.301-4901376.en.html

To convert a string (or longint) into a time value use the Time method: $timeVar:=Time($timeStr)

4D is a typed language and has had a time type since the beginning. However with the addition of ORDA some UI objects no longer support the time type (collection and entity selection list boxes, for example) and use a longint type instead. This can be confusing if you are working with an existing app or older code but attempting to use the newer tools. Be sure to look over

https://developer.4d.com/docs/en/Concepts/time.html

and

https://doc.4d.com/4Dv18/4D/18/Date-and-Time.201-4504355.en.html

You may not need to get so involved in the input. It depends on the nature of the UI and the data. Time and date is tricky in just about every platform.

Upvotes: 1

Related Questions