Page 1 of 2

SOLVED Inputting/Validating Form Data

Posted: Mon Dec 15, 2025 9:20 pm
by MikeGreen99
Ubuntu Unity 24.04 PureBasic 6.21

Hi.

I used the PureBasic built in Form Designer to set up a simple 3 gadget Form; Customer Number (CustNum), Customer Name (CustName), and an Update Button (Button_Update).

I'm trying to find sample code that will demonstrate to me the correct way to input and validate the Fields (gadgets) according to some criteria, like:
CustNum must be 4 digits
CustName can be 3 to 20 characters, A-Z, a-z, or a '.'

When both fields are entered correctly, the 'Update' button would be pressed, the fields cleared, and the next Customer Data would be entered.

The Form .pbf file was generated like this:

Code: Select all

Global CustNum, CustName, Button_Update

Enumeration FormWindow
  #TestFormWindow
EndEnumeration

Enumeration FormGadget
  #Text_1
  #Text_2
EndEnumeration

Declare CustNameProc(EventType)

Procedure OpenTestFormWindow(x = 0, y = 0, width = 720, height = 410)
  OpenWindow(#TestFormWindow, x, y, width, height, "Test Form", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_SizeGadget | #PB_Window_TitleBar | #PB_Window_BorderLess | #PB_Window_ScreenCentered | #PB_Window_WindowCentered | #PB_Window_Maximize | #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_SizeGadget | #PB_Window_TitleBar | #PB_Window_BorderLess | #PB_Window_ScreenCentered | #PB_Window_WindowCentered | #PB_Window_Maximize)
  SetWindowColor(#TestFormWindow, RGB(135,167,0))
  CustNum = StringGadget(#PB_Any, 240, 70, 100, 25, "")
  GadgetToolTip(CustNum, "4 Digits")
  CustName = StringGadget(#PB_Any, 240, 130, 340, 25, "")
  GadgetToolTip(CustName, "3-20 Characters")
  TextGadget(#Text_1, 0, 70, 200, 25, "Customer Number: ")
  TextGadget(#Text_2, 0, 130, 160, 25, "Customer Name:")
  Button_Update = ButtonGadget(#PB_Any, 170, 280, 100, 25, "Update")
EndProcedure

Procedure TestFormWindow_Events(event)
  Select event
    Case #PB_Event_CloseWindow
      ProcedureReturn #False

    Case #PB_Event_Menu
      Select EventMenu()
      EndSelect

    Case #PB_Event_Gadget
      Select EventGadget()
        Case[b] CustName[/b]
          CustNameProc(EventType())          
      EndSelect
  EndSelect
  ProcedureReturn #True
EndProcedure
[/quote]

Questions:
1. I see the Case of CustName, but why no reference to CustNum ?
2. I understand the first 3 lines of the Source Code would be:

[quote]XIncludeFile "TestForm01.pbf" 

OpenTestFormWindow()

While WaitWindowEvent() ! #PB_Event_CloseWindow : Wend
but what would the rest of the Source Code be ?

3. In my old Main Frame programming days, I would have Numbered Fields. You selected the Number Of the Field, entered the data, and hit carriage return. Then there would be a validation process. If there were any invalid characters (a letter instead of a number, etc.), an error message would be displayed, and you would have to re-enter the field.

I do not know how to do this in PB. I gather an event is triggered when a character is entered for any gadget. How can I capture and validate that character ?

And how would I know if someone leaves a Field (gadget) prematurely before it is complete (like only entering 2 characters for the Name gadget) and then clicking on another gadget ?


Is there any sample code that would demonstrate to me on how Fields (gadgets) are inputted and validated ?

So much to learn.

Thanks,
M.....

Re: Inputting/Validating Form Data

Posted: Tue Dec 16, 2025 4:05 am
by TI-994A
MikeGreen99 wrote: Mon Dec 15, 2025 9:20 pm...demonstrate to me the correct way to input and validate the Fields (gadgets) according to some criteria, like:
CustNum must be 4 digits
CustName can be 3 to 20 characters, A-Z, a-z, or a '.'
... And how would I know if someone leaves a Field (gadget) prematurely before it is complete (like only entering 2 characters for the Name gadget) and then clicking on another gadget ?


Something like this:

Code: Select all

XIncludeFile "TestForm01.pbf"

Procedure CustNameProc(e)
  ; defining to conform to form declaration but not used
EndProcedure

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

Procedure validateNumericInput(numericInput.s)
  match = #False
  If CreateRegularExpression(0, "^[0-9]{4}$")
    If MatchRegularExpression(0, Trim(numericInput))
      match = #True
    EndIf
  EndIf  
  ProcedureReturn match  
EndProcedure

OpenTestFormWindow()

Repeat 
  event = WaitWindowEvent()
  Select event
    Case #PB_Event_CloseWindow
      appQuit = #True
    Case #PB_Event_Gadget
      Select EventGadget()  
        Case Button_Update
          If validateNumericInput(GetGadgetText(CustNum)) And validateAlphaInput(GetGadgetText(CustName))
            MessageRequester("Validator", "Inputs are valid.")
          Else
            MessageRequester("Validator", "Inputs are not valid.")
          EndIf
        Case CustNum
          Select EventType()
            Case #PB_EventType_LostFocus
              If Not validateNumericInput(GetGadgetText(CustNum))
                Debug "Please input a valid number."
              EndIf              
          EndSelect
        Case CustName
          Select EventType()
            Case #PB_EventType_LostFocus
              If Not validateAlphaInput(GetGadgetText(CustName))
                Debug "Please input a valid string."
              EndIf              
          EndSelect          
      EndSelect
  EndSelect    
Until appQuit

Re: Inputting/Validating Form Data

Posted: Tue Dec 16, 2025 7:32 pm
by spikey
The StringGadget has the '#PB_String_Numeric' property which will constrain the gadget to positive integers.

You can also set a maximum length on a string gadget via SetGadgetAttribute, this will stop too many characters being entered:

Code: Select all

SetGadgetAttribute(CustName, #PB_String_MaximumLength, 4)
However, you can't set attributes like this via the Form Designer at present so you'd have to put it in your other code somewhere.

You can also change the current gadget selection using SetActiveGadget, so for example your submit button could move the focus back to an incorrect field automatically which will improve the user's experience.

Expanding on Syed's example:

Code: Select all

Global CustNum, CustName, Button_Update

Enumeration FormWindow
  #TestFormWindow
EndEnumeration

Enumeration FormGadget
  #Text_1
  #Text_2
EndEnumeration

Declare CustNameProc(EventType)

Procedure OpenTestFormWindow(x = 0, y = 0, width = 720, height = 410)
  OpenWindow(#TestFormWindow, x, y, width, height, "Test Form", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_SizeGadget | #PB_Window_TitleBar | #PB_Window_BorderLess | #PB_Window_ScreenCentered | #PB_Window_WindowCentered | #PB_Window_Maximize | #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_SizeGadget | #PB_Window_TitleBar | #PB_Window_BorderLess | #PB_Window_ScreenCentered | #PB_Window_WindowCentered | #PB_Window_Maximize)
  SetWindowColor(#TestFormWindow, RGB(135,167,0))
  CustNum = StringGadget(#PB_Any, 240, 70, 100, 25, "", #PB_String_Numeric)
  GadgetToolTip(CustNum, "4 Digits")
  CustName = StringGadget(#PB_Any, 240, 130, 340, 25, "")
  GadgetToolTip(CustName, "3-20 Characters")
  TextGadget(#Text_1, 0, 70, 200, 25, "Customer Number: ")
  TextGadget(#Text_2, 0, 130, 160, 25, "Customer Name:")
  Button_Update = ButtonGadget(#PB_Any, 170, 280, 100, 25, "Update")
EndProcedure

Procedure TestFormWindow_Events(event)
  Select event
    Case #PB_Event_CloseWindow
      ProcedureReturn #False

    Case #PB_Event_Menu
      Select EventMenu()
      EndSelect

    Case #PB_Event_Gadget
      Select EventGadget()
        Case CustName
          CustNameProc(EventType())          
      EndSelect
  EndSelect
  ProcedureReturn #True
EndProcedure

OpenTestFormWindow()

Procedure CustNameProc(e)
  ; defining to conform to form declaration but not used
EndProcedure

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

Procedure validateNumericInput(numericInput.s)
  match = #False
  If CreateRegularExpression(0, "^[0-9]{4}$")
    If MatchRegularExpression(0, Trim(numericInput))
      match = #True
    EndIf
  EndIf  
  ProcedureReturn match  
EndProcedure

OpenTestFormWindow()

; Set maximum lengths.
SetGadgetAttribute(CustNum, #PB_String_MaximumLength, 4)
SetGadgetAttribute(CustName, #PB_String_MaximumLength, 20)

Repeat 
  event = WaitWindowEvent()
  Select event
    Case #PB_Event_CloseWindow
      appQuit = #True
    Case #PB_Event_Gadget
      Select EventGadget()  
        Case Button_Update
          If Not(validateNumericInput(GetGadgetText(CustNum)))
            SetActiveGadget(CustNum)
            MessageRequester("Validator", "Customer numbers must be 4 digits.")
            
          ElseIf Not(validateAlphaInput(GetGadgetText(CustName)))
            SetActiveGadget(CustName)
            MessageRequester("Validator", "Customer names may only contain between 3 and 20 A-Z or period characters.")
            
          Else
            MessageRequester("Validator", "Inputs are valid.")
            
          EndIf
          
        Case CustNum
          Select EventType()
            Case #PB_EventType_LostFocus
              If Not(validateNumericInput(GetGadgetText(CustNum)))
                Debug "Please input a valid number."
              EndIf              
          EndSelect
        Case CustName
          Select EventType()
            Case #PB_EventType_LostFocus
              If Not(validateAlphaInput(GetGadgetText(CustName)))
                Debug "Please input a valid string."
              EndIf              
          EndSelect          
      EndSelect
  EndSelect    
Until appQuit

Re: Inputting/Validating Form Data

Posted: Tue Dec 16, 2025 7:41 pm
by MikeGreen99
Totally Awesome TI-994A ยป :D !

Works like a charm. The only thing that doesn't work is:
Case #PB_EventType_LostFocus
If I enter a Customer Number 2h45 and click to another field, I would have thought it would flag the error, and make me re-enter the Field.
It does not do that - it will allow me to enter the next field. Then when I click 'Update', the 'Inputs Are Not Valid' message is displayed.

Not important, just curious as to why this is.

Thanks and have a nice day,
M....

Re: Inputting/Validating Form Data

Posted: Tue Dec 16, 2025 8:14 pm
by TI-994A
MikeGreen99 wrote: Tue Dec 16, 2025 7:41 pm...If I enter a Customer Number 2h45 and click to another field, I would have thought it would flag the error, and make me re-enter the Field.

Just force the focus back to the erring input:

Code: Select all

XIncludeFile "TestForm01.pbf"

Procedure CustNameProc(e)
  ; defining to conform to form declaration but not used
EndProcedure

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

Procedure validateNumericInput(numericInput.s)
  match = #False
  If CreateRegularExpression(0, "^[0-9]{4}$")
    If MatchRegularExpression(0, Trim(numericInput))
      match = #True
    EndIf
  EndIf  
  ProcedureReturn match  
EndProcedure

OpenTestFormWindow()

Repeat 
  event = WaitWindowEvent()
  Select event
    Case #PB_Event_CloseWindow
      appQuit = #True
    Case #PB_Event_Gadget
      Select EventGadget()  
        Case Button_Update
          If validateNumericInput(GetGadgetText(CustNum)) And validateAlphaInput(GetGadgetText(CustName))
            MessageRequester("Validator", "Inputs are valid.")
          Else
            MessageRequester("Validator", "Inputs are not valid.")
          EndIf
        Case CustNum
          Select EventType()
            Case #PB_EventType_LostFocus
              If Not Trim(GetGadgetText(CustNum)) = "" And Not validateNumericInput(GetGadgetText(CustNum))                
                Debug "Please input a valid number."
                SetActiveGadget(CustNum)
              EndIf              
          EndSelect
        Case CustName
          Select EventType()
            Case #PB_EventType_LostFocus
              If Not Trim(GetGadgetText(CustName)) = "" And Not validateAlphaInput(GetGadgetText(CustName))
                Debug "Please input a valid string."
                SetActiveGadget(CustName)
              EndIf              
          EndSelect          
      EndSelect
  EndSelect    
Until appQuit

Re: Inputting/Validating Form Data

Posted: Tue Dec 16, 2025 8:20 pm
by MikeGreen99
Hi spikey.

I'm sort of confused. This code seems more comprehensive as it better refines the input - but isn't this the .pbf Form, and will it not be
automatically re-generated by the Form Designer ?

Thanks,
M...

Re: Inputting/Validating Form Data

Posted: Tue Dec 16, 2025 8:35 pm
by MikeGreen99
Hi TI-994A.

Much better. If I enter an Invalid Customer Number and click to the next field, the erroneous Customer Number is highlighted - no error message is displayed. I have no problem with that.

However, for some reason the Tooltips are not being displayed. I would think if I hover my mouse over the StringGadget Fields a Tooltip would appear.
In the Form Designer, I've entered Tooltips for the Text and their corresponding StringGadget Fields - no Tooltip is displayed when I hover over any of them.

Thanks,
M...

Re: Inputting/Validating Form Data

Posted: Tue Dec 16, 2025 8:46 pm
by TI-994A
MikeGreen99 wrote: Tue Dec 16, 2025 8:35 pm...the Tooltips are not being displayed. I would think if I hover my mouse over the StringGadget Fields a Tooltip would appear.

Seems to work:
Image

Re: Inputting/Validating Form Data

Posted: Tue Dec 16, 2025 8:57 pm
by spikey
MikeGreen99 wrote: Tue Dec 16, 2025 8:20 pm Hi spikey.

I'm sort of confused. This code seems more comprehensive as it better refines the input - but isn't this the .pbf Form, and will it not be
automatically re-generated by the Form Designer ?

Thanks,
M...
My apologies, I didn't intend to confuse. Yes, I expanded the code as a self contained runnable example of the features I mentioned. It's cumbersome to try and post a "this is the form and this is the extra code" sample in the forum. You would need to fit this code into your program at appropriate points, apart from the #PB_String_Numeric flag which you can set in the Form Designer.

Re: Inputting/Validating Form Data

Posted: Wed Dec 17, 2025 10:31 pm
by MikeGreen99
Hey guys.

I'm slowly learning.

1. For whatever reason, Tooltips do not work on my Form ??

2. What would the Source Code Procedure look like to validate an Integer in the range of say -750 ----> +34567 ?

3. What would the Source Code Procedure look like to validate an entered Date in the form of yyyymmdd ?
So an entry of 20251322 would be Invalid.

4. If I were to build a string of Months Of the Year: Mon$="janfebmaraprmayjunjulaugsepoctnovdec"
What would the Source Code Procedure look like to validate a valid Month ?
So an entry of 'abc' would be invalid.

Thanks,
M....

Re: Inputting/Validating Form Data

Posted: Wed Dec 17, 2025 11:38 pm
by pfnuesu
Hi M.

1. Sorry, no clue :wink:

2.

Code: Select all

x = -75

If x >= -750 And x <= 34567 
  Debug "OK " + x 
Else 
  Debug "NOK" 
EndIf
  
3. I would parse the entry into a date function and check if I get someting valid in return:

Code: Select all

Define y.s = "20251317"
;Define y.s = "20251031"  

If ParseDate("%yyyy%mm%dd",y) = -1 
  Debug "NOK: " + y
Else
  Debug "OK: " + FormatDate("%yyyy-%mm-%dd",ParseDate("%yyyy%mm%dd",y))
EndIf

4. For this I'd take the same approach as above (working with date function), but if you insist on checking a string:

Code: Select all

Define mon.s = "janfebmaraprmayjunjulaugsepoctnovdec"
Define tStr.s = "sep" 
Define result.i = 0


For i = 1 To Len(mon) Step 3
  If Mid(mon, i,3) = LCase(tStr)
    result = i 
    Break
  EndIf    
Next i

If result > 0 
  Debug "OK: " + tStr + " @Pos: " + i
Else   
  Debug "NOK: " + tStr
EndIf
Best,
Pfn.

Re: Inputting/Validating Form Data

Posted: Thu Dec 18, 2025 6:42 pm
by MikeGreen99
Hi.

Sorry, my post was misleading.

I meant in the context of inputting/validating Form Data. For example, from this thread, I have learnt how to validate String Input that requires specific characters, and whose length = 3 -->20 characters:
Procedure validateAlphaInput(alphaInput.s)
match = #False
If CreateRegularExpression(0, "^[a-zA-Z .]{3,20}$")
If MatchRegularExpression(0, Trim(alphaInput))
match = #True
EndIf
EndIf
ProcedureReturn match
EndProcedure
In this context:
1. What would the Validation Procedure look like to validate an Integer in the range of say -750 ----> +34567 ?

2. What would the Validation Procedure look like to validate an entered Date in the form of yyyymmdd ?
So an entry of 20251322 would be Invalid.

3. If I were to build a string of valid degrees: Degrees$="BSC.BENG.MBA.BA.PHD.PharmD.MD.Mres.Whatever."
What would the Validation Procedure look like to validate one of these valid degrees ?
So an entry of 'nfe' would be invalid.


All of this valuable info will help me build a generalized template on how input and validate various kinds of data.

Thanks for all the help,
M...

Re: Inputting/Validating Form Data

Posted: Fri Dec 19, 2025 12:18 am
by pfnuesu
? All of the snippets work as validations. They are all executable in IDE with compile/run, so you can play with the input values.

I leave it to you to put them into procedures (see answers above) in form of

Code: Select all

Procedure PROCEDURENAME(VARIABLENAME.Type)
	<code here>

	ProcedureReturn [result-variable-name]
EndProcedure

A heads up for the "validate integer" snippet:

Coming from a form, you'd have to convert the input from text to number so you should encase that one in VAL()


AAAAND: You're third question is different from the one yesteterday. Now we have a "." as delimiter and the entries in your string aren't the same length anymore.

So for this I'd use

Code: Select all

Define tStr.s = "BENG" 

Degrees$="BSC.BENG.MBA.BA.PHD.PharmD.MD.Mres.Whatever."
Define result.i = 0

For k = 1 To CountString(Degrees$, ".")
  If tStr = StringField(Degrees$, k, ".") 
    result = 1
    Break
  EndIf
Next


If result > 0 
   Debug "OK: " + tStr 
 Else   
   Debug "NOK: " + tStr
EndIf

Re: Inputting/Validating Form Data

Posted: Fri Dec 19, 2025 6:16 am
by TI-994A
MikeGreen99 wrote: Thu Dec 18, 2025 6:42 pm...to validate an Integer in the range of say -750 ----> +34567 ?

...to validate an entered Date in the form of yyyymmdd ?

...a string of valid degrees: Degrees$="BSC.BENG.MBA.BA.PHD.PharmD.MD.Mres.Whatever."
What would the Validation Procedure look like to validate one of these valid degrees ?

Here's an early Christmas gift for you, Mike:

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)

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

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)

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)

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)

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)

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!")
          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

I hope it helps. Feliz Navidad! :D

Re: Inputting/Validating Form Data

Posted: Fri Dec 19, 2025 6:55 pm
by MikeGreen99
Thank you ALL and have a Happy Holiday Season :D .
M......