Date validation

I have a string entry field (string (100) and @s100 fromat) and a user can input any date there. I need to validate if the entered value is a valid clarion date, let say, in @d6 format. Is it possible to check it easy?

For example:
1a/12/2020 false
11/2020 false
26/06/2020 true

Might a Windows short date @d17 help?

how it can help???..

Can you simply put a date picture on the field, but remove the ,MASK attribute
IIRC, that way they can enter the date in any format, and it’s converted to the formatted picture

Mark, but user can also input just a normal string in this field as well and I need to validate if it is DATE or just normal string. If it looks like date (so that I need to validate) - then OK, if it normal string - OK.

Use a local date variable to allow date input. Use your string variable for string input.
Resolve final string value prior to saving record.

not sure I understand your idea. I have ONE variable (string) and one Entry control. This is string entry control - users can type whatever they want. All I need to be able to check (validate) if entered info is a DATE

Maybe try this:
add a hidden control with a Date picture and no mask
then push the string value into the control
possibly use PRESS(String)

then check the USE variable of the hidden control
if it’s non-zero then it must be a valid date

Mark, it’s not just screen inputting. I need common function like

CheckDate(my:string) that just check and return False or True

So, write the method, and add a window in the method
Granted it’s not terribly efficient, but it’s a place to start
Clearly the RTL has the functionality built-in somewhere.

A different approach could be to try to parse the string into a date

or DEFORMAT it with every date picture you can think of
trying to find one that returns non-zero

Perhaps the entry can be more structured?
The user can set a check to indicate a date will be entered – or not set to indicate a string will be entered. You then set the control accordingly.
Allowing dates to be validated with a control is going to be easier than doing it on your own.

Mark, yes, I was thinking about parsing…

Douglas, it’s better to have some common function

The following appears in String Theory 3 (and likely older versions too)

!------------------------------------------------------------------------------
! works hard to figure out what the date is from the input.
! simple numbers > 2100 are assumed to be deformatted already, and returned as-is.
StringDeformat.DeformatDate PROCEDURE (String pValue,)

It returns a REAL

1 Like

Mark, nice, I have ST so going to check it…

hmmm… have 2.63 version. And it has no such method… can’t upgrade

As you please. Something else to consider 26 June 20 and July 1, 2020.

Is PRESS() a viable option nowadays?

So you wrote a function but it doesn’t work as expected, right?

Hidden ENTRY with date picture @d05 will convert string ‘1a/12/2020’ to date 01/01/2002 which is valid but not expected result.

Hi Clarion_clarion (maybe change that to your real name so we know who we are talking to!)

so let’s see

  • you want to see if the date is a valid date in @d6 format

  • you have StringTheory 2 but not ST3 and can’t update… (BTW I see the upgrade is only $39 and has many new methods, optimizations and bug fixes so seems like a no brainer to me!)

  • you a want a function where you pass a string and get back true for a valid date otherwise false

@d6 has the format dd/mm/yyyy so off the top of my head I would do something like:

CheckDate  procedure (String pStr)

st   stringTheory
dd   long,auto
mm   long,auto
yyyy long,auto

  code
  st.setValue(pStr)
  st.trim()
  if ~st.isAll('/0123456789') then return false. ! contains invalid characters

  st.split('/')
  if st.records() <> 3 then return false. 

  dd = st.getLine(1)
  if dd < 1 or dd > 31 then return false.

  mm = st.getLine(2)
  if mm < 1 or mm > 12 then return false.
  if dd = 31 and inlist(mm,2,4,6,9,11) then return false.

  yyyy = st.getLine(3)
  if ~yyyy then return false.

  ! note: you may wish to allow a 2 digit year and 
  ! guess the century, for example:
  if yyyy < 25
    yyyy += 2000
  elsif yyyy < 100
    yyyy += 1900
  end

  ! you may wish to do range checks on year here...
  ! for example:
  if yyyy < 1800 or yyyy > 2100 then return false.
  
  if mm <> 2 or dd < 29 then return true.  ! valid date...

  ! special tests for February
  if dd = 30 then return false.
  
  ! dd is 29 so check for leap year
  if yyyy%4   then return false.
  if yyyy%100 then return true.
  if yyyy%400 then return false.

  return true

well I wouldn’t quite do that - inlist() is very expensive and so I probably would use case instead but this keeps it on one line…hope that helps

Geoff R