Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check the date if Valid in VFP6

Status
Not open for further replies.

ellehcsim

Programmer
Dec 9, 2003
16
0
0
PH
hi guys!

is there a special function in VFP6 that checks if the user inputs a valid date.

>>> function that works something like this.

if IFDATE(thisform.text1.value) = .t.
... do something ...
else
messagebox("Invalid date.")
endif


thank you!!
 

ellehcsim,

in your form's init event, put thisform.text1.value=date(), and foxpro automatically checks if the entered date is valid or not. foxpro will give you a message "invalid date" if you entered an invalid date.

HTH
 
to add on what R17 mentioned, that also depends on your date settings.

kilroy [trooper]
philippines
"and that's what we call creativity..."
 
You can do it the way you want, too:

Code:
lcDate = '{'+thisform.text1.value+'}'
if TYPE(lcDate) = 'D'
  ... The date is valid

However, in VFP6+ you might start getting errors because of the SET STRICTDATE setting, which encourages only using the format {^ YYYY/MM/DD} for dates.

So, you can parse the value itself, and put it in that format:

Assuming the user types MM/DD/YYYY:

Code:
lcInput = thisform.text1.value
lcDate = '{^ '+ SUBSTR(lcInput,7,4) ;
        +'/'  + SUBSTR(lcInput,1,2) ;
        +'/'  + SUBSTR(lcInput,4,2) 
if TYPE(lcDate) = 'D'
  ... The date is valid


The issue is that some countries usually type the date:
MM/DD/YY
others: DD/MM/YY
and others use other formats entirely:
So how is VFP to know which number is the month and which the day? It had a built in setting (SET DATE TO American) for the countries, then CTOD() and DTOC() would use that format, but it apparently introduced so many problems that VFP's SET STRICTDATE effectively deprecates those three commands/functions.

You can, of course, create your own function wrapping SET DATE, TYPE() and CTOD() checks between SET STRICTDATE commands to specifically choose a date using a specific format.

 
On the text field - go to the propeties and set the VALUE to {}

Now VFP will handle it as a date type and will handle invalid date types.

Jim Osieczonek
Delta Business Group, LLC
 
Here is a working example, it incorporates some of the ideas already expressed and includes a good use for the ON READERROR statement. Cut-N-Paste this code into a prg and run it from within VFP.

Code:
PUBLIC oForm
oForm = CREATEOBJECT("clsValidateDates")
oForm.show()

DEFINE CLASS clsvalidatedates AS form
	Top = 0
	Left = 0
	Height = 250
	Width = 399
	DoCreate = .T.
	Caption = "Validate Dates"
	Name = "Form1"
	AutoCenter = .T.
	
	ADD OBJECT text1 AS textbox WITH ;
		Alignment = 3, ;
		Value = {}, ;
		Height = 23, ;
		Left = 48, ;
		Top = 69, ;
		Width = 100, ;
		Name = "Text1"

	ADD OBJECT text2 AS textbox WITH ;
		Height = 23, ;
		Left = 48, ;
		Top = 192, ;
		Width = 100, ;
		Name = "Text2"

	ADD OBJECT label1 AS label WITH ;
		WordWrap = .T., ;
		BackStyle = 0, ;
		Caption = "First textbox created with {} as it's value.  VFP will handle validating the date, however we will override the 'Invalid Date' message to give the user more information.", ;
		Height = 48, ;
		Left = 48, ;
		Top = 21, ;
		Width = 312, ;
		ForeColor = RGB(128,0,0), ;
		Name = "Label1"

	ADD OBJECT label2 AS label WITH ;
		WordWrap = .T., ;
		BackStyle = 0, ;
		Caption = "Second textbox created without a default value, will allow the user to enter the date in a variety of ways.  We will validate the user's entry in code by using the CTOD() function.", ;
		Height = 48, ;
		Left = 48, ;
		Top = 144, ;
		Width = 312, ;
		ForeColor = RGB(128,0,0), ;
		Name = "Label2"

	PROCEDURE text1.LostFocus
		ON READERROR
	ENDPROC

	PROCEDURE text1.GotFocus
		ON READERROR Messagebox("The date you have entered is either" + CHR(13) + ;
								"a bad date (such as 02/31/2004 or 01/00/2004)" + CHR(13) + ;
								"or formatted incorrectly (enter dates as mm/dd/yyyy).",16,"Invalid Date")
	ENDPROC

	PROCEDURE text2.Valid
		LOCAL ldDateEntered
		IF !EMPTY(this.Value)
			ldDateEntered = CTOD(this.Value)
			IF EMPTY(ldDateEntered)
				MESSAGEBOX("The date you have entered does not appear to be valid." + CHR(13) + ;
							"Re-enter the date such as 01-31-2004 or 01/31/04.",16,"Invalid Date")
				RETURN 0
			ELSE
				this.Value = DTOC(ldDateEntered)
			ENDIF
		ENDIF
	ENDPROC

ENDDEFINE

Slighthaze = NULL
[sub]craig1442@mchsi.com[/sub][sup]
"Whom computers would destroy, they must first drive mad." - Anon​
[/sup]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top