Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Validate controls or fields
Message
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00667196
Message ID:
00667210
Views:
23
Hi joe!!
>Is there a recommended method to validating data on a form?

If I were you I would not Put the validation on the data or the textbox.
Create a method that handles all of your validatation.

Step 1:
Create a property on your form that will contain all names of the fields that you need to validate ie thisform.r_validFields. you would just put the names in there like:
txtField1,txtField2,txtfield3, etc

Step2:
Create a method that will handle the validation by excepting the string from thisform.r_validation.
ie thisform.m_validateFields(thisform.r_validFields)


Step3:
The Code could loop through the string and return a generic message
LPARAMETER ValString
MyString = ValString

#DEFINE Line1Message "Invalid Entry " + CHR(13)
#DEFINE Line2Message "Click OK to correct Invalid Entry" 

** find out to see how long the string is
myLen = LEN(MYSTRING)
*** Find out how many controls we have
NumLoops = OCCURS(',',Mystring) + 1

FOR lnCount = 1  TO NumLoops
    
    ComPosition = AT(',',MyString)
    IF ComPosition = 0
        ThisValidField = MyString
        MYSTRING = ''

    ELSE
        ThisValidField = LEFT(MyString,ComPosition - 1) 
        myLen = LEN(MyString)     
        MYSTRING = RIGHT(MYSTRING,MyLen - ComPosition)

    ENDIF    
    ThisValidFieldValue = 'THISFORM.'+ALLTRIM( ThisValidField) + '.VALUE'
    IF EMPTY(&ThisValidFieldValue)       
       MESSAGEBOX(Line1Message+Line2Message,64,'Error')
       ThisValidSetFocus = 'THISFORM.' + ALLTRIM( ThisValidField) + '.SETFOCUS()'
       &ThisValidSetFocus
       RETURN .F.
    ENDIF 
       
ENDFOR
This of Course is very generic. If you wanted to be mor specific you could try this:

Step1:
Create a method for validatation. ie thisform.m_validateSpecificField
this method would have everything you would do given a specific control name and label caption.
   *** Method m_ValidateSpecificField
   lparameters tcControlName,tcWeWillCallItThis
   IF PCOUNT() < 1
     Return
   ELSE
     do case
        case tcControlName = 'SomeControlOnTheForm'
           *** do some specific Stuff
           *** Send a message back using name contained in tcWeWillCallItThis
           *** set the focus back to that control (SomeControlOnTheForm)
        case tcControlName = 'AnotherControlOnTheForm'
           *** do some specific Stuff
           *** Send a message back using name contained in tcWeWillCallItThis
           *** set the focus back to that control (SomeControlOnTheForm)
   && Do this until all of your specific Code is accounted for

     endcase
   ENDIF
If you put your Validation in a method or a Com object it is Much easer to maintain if you should decide to change something down the road.

HTH
What is Wisdom?
Wisdom - The ability to respond to any situation according to God's Plan.
Therefore:
USE Wisdom IN Everything ORDER priority AS Knowledge
SELECT Knowledge
BROWSE FOR Understanding WHERE Wisdom=Guide

LeRoy Jackson
Previous
Reply
Map
View

Click here to load this message in the networking platform