SOLVED Inputting/Validating Form Data

Just starting out? Need help? Post your questions and find answers here.
MikeGreen99
User
User
Posts: 23
Joined: Wed Dec 03, 2025 5:40 pm

Re: Inputting/Validating Form Data

Post by MikeGreen99 »

Hey TI-994A

I have been playing with your Xmas Gift code and learning a lot.

I want to be able to write a screenful of data to a file, and then continue on to the next screenful of data, etc., until the Terminate button is hit.

After the Submit Button is successfully pressed, I added code to capture the Gadget Data so they can be written to a file .
I then initialized the gadgets (set = "") in preparation for the next screenful of data.
I then SetActiveGadget(nameInput) so the first field is in focus.
All good.

The problem I have is how to Clear all the gadgets being displayed from the previous entry ? I have initialized the variables, but the 'old' Data is still
displayed. Can I Loop back to the beginning each time the Submit Button is pressed ?

Thanks,
M..
User avatar
mk-soft
Always Here
Always Here
Posts: 6479
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Inputting/Validating Form Data

Post by mk-soft »

I don't know what you mean by initialising variables. Filling variables with a defined value?
Changing variables does not change gadgets. Changing the variables of gadgets with the type #PB_Any is not a good idea, as you will then no longer have access to the gadgets.

To initialise the content of gadgets, you must edit all gadgets with SetGadgetText, SetGadgetState or ClearGadgetItems.
The best way to do this is to write procedures.
- ClearData()
- SetData()
- GetData()
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
TI-994A
Addict
Addict
Posts: 2789
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Inputting/Validating Form Data

Post by TI-994A »

MikeGreen99 wrote: Sat Dec 20, 2025 10:26 pm...how to Clear all the gadgets being displayed from the previous entry ?

Each input field has to be cleared manually, with the SetGadgetText() function. I've updated the previous code to do this in a slightly automated manner, by assigning the input gadget numbers to an array, then looping through the array to clear them.

Code: Select all

;==========================================================
;
;   Simple input form validation example for 
;   names, numbers, dates, and string matching
;      
;   tested & working with PureBasic 6.21 (arm64) on
;   Windows 11, macOS Sequoia, and Ubuntu Linux 25.04
;
;   by TI-994A - free to use, improve, share...
;
;   19th December 2025 - Singapore
;
;==========================================================

EnableExplicit

Declare validateAll()
Declare validName(alphaInput.s)
Declare validDegree(alphaInput.s)
Declare validScore(numericInput.s)
Declare validDate(numericInput.s)
Declare clearForm(Array gadgetList(1)) 

Define nameLabel, nameInput, nameErr, degreeLabel, degreeInput, degreeErr, 
       scoreLabel, scoreInput, scoreErr, dateLabel, dateInput, dateErr,
       validatingName, validatingDegree, validatingScore, validatingDate,
       winMain, submitButton, errMsgFont, appQuit, gCount

Dim inputGadgetList (3)

errMsgFont = FontID(LoadFont(#PB_Any, "Arial", 10))

winMain = OpenWindow(#PB_Any, 0, 0, 400, 300, "Form Validator", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

nameLabel = TextGadget(#PB_Any, 30, 30, 80, 20, "Name:")
nameInput = StringGadget(#PB_Any, 120, 30, 250, 20, "")
nameErr = TextGadget(#PB_Any, 120, 50, 250, 15, "please enter a valid name", #PB_Text_Right)
SetGadgetColor(nameErr, #PB_Gadget_FrontColor, #Red)
SetGadgetFont(nameErr, errMsgFont)
HideGadget(nameErr, #True)
inputGadgetList(gCount) = nameInput
gCount + 1

degreeLabel = TextGadget(#PB_Any, 30, 80, 80, 20, "Degree:")
degreeInput = StringGadget(#PB_Any, 120, 80, 250, 20, "")
degreeErr = TextGadget(#PB_Any, 120, 100, 250, 15, "please enter a valid degree name", #PB_Text_Right)
SetGadgetColor(degreeErr, #PB_Gadget_FrontColor, #Red)
SetGadgetFont(degreeErr, errMsgFont)
HideGadget(degreeErr, #True)
inputGadgetList(gCount) = degreeInput
gCount + 1

scoreLabel = TextGadget(#PB_Any, 30, 130, 80, 20, "Score:")
scoreInput = StringGadget(#PB_Any, 120, 130, 250, 20, "")
scoreErr = TextGadget(#PB_Any, 120, 150, 250, 15, "please enter a valid score (-750 to +34,567)", #PB_Text_Right)
SetGadgetColor(scoreErr, #PB_Gadget_FrontColor, #Red)
SetGadgetFont(scoreErr, errMsgFont)
HideGadget(scoreErr, #True)
inputGadgetList(gCount) = scoreInput
gCount + 1

dateLabel = TextGadget(#PB_Any, 30, 180, 80, 20, "Date:")
dateInput = StringGadget(#PB_Any, 120, 180, 250, 20, "", #PB_String_Numeric)
dateErr = TextGadget(#PB_Any, 120, 200, 250, 15, "please enter a valid date (yyyymmdd)", #PB_Text_Right)
SetGadgetColor(dateErr, #PB_Gadget_FrontColor, #Red)
SetGadgetFont(dateErr, errMsgFont)
HideGadget(dateErr, #True)
inputGadgetList(gCount) = dateInput
gCount + 1

submitButton = ButtonGadget(#PB_Any, 115, 240, 150, 25, "Submit")

SetActiveGadget(nameInput)

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      appQuit = #True 
      
    Case #PB_Event_Gadget
      Select EventGadget()
        Case nameInput          
          If EventType() = #PB_EventType_LostFocus                        
            If Not validatingDegree And Not validatingScore And Not validatingDate
              If Not validName(GetGadgetText(nameInput))
                validatingName = #True
                HideGadget(nameErr, #False)                              
                SetActiveGadget(nameInput)
              Else
                HideGadget(nameErr, #True)                              
                validatingName = #False
              EndIf            
            EndIf            
          EndIf
          
        Case degreeInput          
          If EventType() = #PB_EventType_LostFocus
            If Not validatingName And Not validatingScore And Not validatingDate
              If Not validDegree(GetGadgetText(degreeInput))
                validatingDegree = #True
                HideGadget(degreeErr, #False)                              
                SetActiveGadget(degreeInput)
              Else
                HideGadget(degreeErr, #True)                              
                validatingDegree = #False
              EndIf                          
            EndIf            
          EndIf
          
        Case scoreInput
          If EventType() = #PB_EventType_LostFocus
            If Not validatingName And Not validatingDegree And Not validatingDate
              If Not validScore(GetGadgetText(scoreInput))
                validatingScore = #True
                HideGadget(scoreErr, #False)                              
                SetActiveGadget(scoreInput)
              Else
                HideGadget(scoreErr, #True)                              
                validatingScore = #False
              EndIf                                      
            EndIf
          EndIf
          
        Case dateInput
          If EventType() = #PB_EventType_LostFocus
            If Not validatingName And Not validatingDegree And Not validatingScore
              If Not validDate(GetGadgetText(dateInput))
                validatingDate = #True
                HideGadget(dateErr, #False)                              
                SetActiveGadget(dateInput)
              Else
                HideGadget(dateErr, #True)                              
                validatingDate = #False
              EndIf                                      
            EndIf
          EndIf
          
        Case submitButton          
          If validateAll()
            MessageRequester("Form Validator", "Form submitted successfully!")
            clearForm(inputGadgetList())
          Else            
            MessageRequester("Form Validator", "Please input the required fields.")
          EndIf
          
      EndSelect 
  EndSelect
Until appQuit

Procedure validName(alphaInput.s) 
  Protected match = #False
  If CreateRegularExpression(0, "^[a-zA-Z .]{3,20}$")
    If MatchRegularExpression(0, Trim(alphaInput))
      match = #True
    EndIf
  EndIf  
  ProcedureReturn match  
EndProcedure

Procedure validDegree(alphaInput.s) 
  Protected match = #False
  Protected.s degrees =".BSC.BENG.MBA.BA.PHD.PharmD.MD.Mres."  
  If FindString(LCase(degrees), "." + LCase(Trim(alphaInput)) + ".")
    match = #True
  EndIf  
  ProcedureReturn match  
EndProcedure

Procedure validScore(numericInput.s)  
  Protected numericInputVal, valid = #False      
  If CreateRegularExpression(0, "^[+-]?\d+$")
    If MatchRegularExpression(0, Trim(numericInput))
      numericInputVal = Val(numericInput)
      If numericInputVal >= -750 And numericInputVal <= 34567
        valid = #True
      EndIf      
    EndIf
  EndIf    
  ProcedureReturn valid     
EndProcedure

Procedure validDate(numericInput.s)
  Protected valid = #False
  If ParseDate("%yyyy%mm%dd", Trim(numericInput)) <> -1
    valid = #True
  EndIf
  ProcedureReturn valid  
EndProcedure

Procedure validateAll()
  Protected validated = #False
  Shared nameInput, degreeInput, scoreInput, dateInput,
         nameErr, degreeErr, scoreErr, dateErr
  If validName(GetGadgetText(nameInput)) And
     validDegree(GetGadgetText(degreeInput)) And
     validScore(GetGadgetText(scoreInput)) And
     validDate(GetGadgetText(dateInput))
    HideGadget(nameErr, #True) 
    HideGadget(degreeErr, #True) 
    HideGadget(scoreErr, #True) 
    HideGadget(dateErr, #True) 
    validated = #True
  EndIf
  ProcedureReturn validated        
EndProcedure

Procedure clearForm(Array gadgetList(1)) 
  Protected i  
  For i = 0 To ArraySize(gadgetList())
    SetGadgetText(gadgetList(i), "")
  Next i  
EndProcedure

Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
MikeGreen99
User
User
Posts: 23
Joined: Wed Dec 03, 2025 5:40 pm

Re: Inputting/Validating Form Data

Post by MikeGreen99 »

Hey TI-994A: Works Like A Charm :D !!

Hey mk-soft: I realize now that I do not have to initialize any variables - my bad.

You all have been a SUPER BIG help. So much to learn. It would have taken me forever to find statements like "CreateRegularExpression", etc.

How do I mark this post as Solved ?

Thanks & Happy Holidays,
M.....
User avatar
mk-soft
Always Here
Always Here
Posts: 6479
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Inputting/Validating Form Data

Post by mk-soft »

Edit the title in the first post.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
TI-994A
Addict
Addict
Posts: 2789
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Inputting/Validating Form Data

Post by TI-994A »

MikeGreen99 wrote: Sun Dec 21, 2025 7:05 pmHey TI-994A: Works Like A Charm :D !!

So glad to hear it. Happy holidays to you too. :D
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
Post Reply