McBasic™Get McBasic™ ↗
Build 881
Hands-on tutorial · Input & events

Display field-level validation

Combine built-in validity with a business rule attached to a named control.

1 Form / control designs1 complete BASIC filesGuided sample project

valFields.Target selects which field receives ErrorText. Setting ErrorText to an empty string clears that field’s previous message. The age rule is applied only after numAge.IsValid succeeds. A final status should summarize the result, while the individual field retains the reason it needs attention.

Step 1Prepare a safe practice project#

Extract the archive into a new writable folder, separate from your own application. Open ValidationProvider.mcproj in McBasic™. Keep every declared source, layout, resource and migration file together.

To reconstruct it yourself, create a DesktopApp project and add the files declared below. In the complete project, follow these steps as a guided rebuild: replace each named source file once and Check after all its procedures are present.

Exact project configuration
ValidationProvider.mcproj
{
  "schemaVersion": 1,
  "projectId": "591ed385-26a9-4e20-89f7-dd22aa261026",
  "name": "ValidationProvider",
  "kind": "DesktopApp",
  "language": {
    "dialect": "McBasic1",
    "requireExplicit": true,
    "compare": "Binary",
    "defaultArrayBase": 0
  },
  "application": {
    "bundleIdentifier": "com.mcbasic.samples.validationprovider",
    "version": "0.1.0",
    "buildNumber": "1",
    "startupFormId": "form-main",
    "shutdownMode": "OnStartupFormClosed"
  },
  "forms": [
    {
      "id": "form-main",
      "typeName": "frmMain",
      "layout": "Forms/frmMain.form.json",
      "code": "Forms/frmMain.bas"
    }
  ],
  "modules": [],
  "classes": [],
  "tests": [],
  "references": [],
  "resources": [],
  "targets": [
    "osx-arm64"
  ],
  "capabilities": []
}

Step 2Build the Form and set its properties#

frmMain

Select the Form first and set its properties. Add containers before their children. These are initial Designer values, not statements to paste into a procedure.

Form propertyValue
Caption"ValidationProvider — McBasic"
Height440
Width520
TypeNameParent
TextBoxtxtNameForm
TextBoxtxtEmailForm
NumericBoxnumAgeForm
MaskedTextBoxmtxPhoneForm
LabellblStatusForm
CommandButtoncmdValidateForm
ValidationProvidervalFieldsComponent tray
txtName · exact Inspector values
PropertyValue
AccessibleName"Customer name"
EnabledTrue
Height32
Left24
MultiLineFalse
ReadOnlyFalse
TabIndex0
Text""
Top24
VisibleTrue
Width440
WordWrapFalse
RequiredTrue
Anchor edgesLeft, Top
txtEmail · exact Inspector values
PropertyValue
AccessibleName"Email address"
EnabledTrue
Height32
Left24
MultiLineFalse
ReadOnlyFalse
TabIndex1
Text""
Top80
VisibleTrue
Width440
WordWrapFalse
MaxLength80
RequiredTrue
Anchor edgesLeft, Top
numAge · exact Inspector values
PropertyValue
Left24
Top136
Width180
Height32
TabIndex2
Minimum0
Maximum100
Value18
Step1
DecimalPlaces0
Culture"Invariant"
Anchor edgesLeft, Top
mtxPhone · exact Inspector values
PropertyValue
Left24
Top192
Width220
Height32
TabIndex3
Mask"000-0000"
PromptChar"_"
Text""
Anchor edgesLeft, Top
lblStatus · exact Inspector values
PropertyValue
Caption"Enter a name, email and optional phone, then choose Validate."
EnabledTrue
Height64
Left24
TabIndex4
Top248
VisibleTrue
Width440
Anchor edgesLeft, Top
cmdValidate · exact Inspector values
PropertyValue
Caption"Validate fields"
EnabledTrue
Height32
Left24
TabIndex5
Top336
VisibleTrue
Width220
Anchor edgesLeft, Top
valFields · exact Inspector values
PropertyValue
Target""
ErrorText""

Step 3Connect the events explicitly#

frmMain

ObjectEventProcedureParameters
cmdValidateClickcmdValidate_ClickClick()

Step 4Enter the complete behavior#

Open the named source file. Enter declarations first, then every procedure. Expand a procedure for its full body, or copy the complete file. Helpers are required: do not stop after entering the first Click handler.

Forms/frmMain.bas

Enter these declarations at file scope, before the procedures.

Forms/frmMain.bas · declarations
Option Explicit
Private Sub cmdValidate_Click()

Enter the complete action, including its input checks and calls to helper procedures.

Forms/frmMain.bas · cmdValidate_Click
Private Sub cmdValidate_Click()
    valFields.Target = "txtEmail"
    If Len(Trim(txtEmail.Text)) > 0 And Len(Trim(txtEmail.Text)) < 3 Then
        valFields.ErrorText = "Email is too short."
    Else
        valFields.ErrorText = ""
    End If

    valFields.Target = "numAge"
    valFields.ErrorText = ""
    If numAge.IsValid Then
        If numAge.Value < 18 Then
            valFields.ErrorText = "Age must be at least 18."
        End If
    End If

    If mtxPhone.IsValid And valFields.IsValid Then
        lblStatus.Caption = "Ready to save."
    Else
        lblStatus.Caption = CStr(valFields.ErrorCount) & " field(s) need attention: " & valFields.FirstError
    End If
End Sub
Copy the complete Forms/frmMain.bas file

Replace the file once; do not append duplicate procedures.

Forms/frmMain.bas
Option Explicit

Private Sub cmdValidate_Click()
    valFields.Target = "txtEmail"
    If Len(Trim(txtEmail.Text)) > 0 And Len(Trim(txtEmail.Text)) < 3 Then
        valFields.ErrorText = "Email is too short."
    Else
        valFields.ErrorText = ""
    End If

    valFields.Target = "numAge"
    valFields.ErrorText = ""
    If numAge.IsValid Then
        If numAge.Value < 18 Then
            valFields.ErrorText = "Age must be at least 18."
        End If
    End If

    If mtxPhone.IsValid And valFields.IsValid Then
        lblStatus.Caption = "Ready to save."
    Else
        lblStatus.Caption = CStr(valFields.ErrorCount) & " field(s) need attention: " & valFields.FirstError
    End If
End Sub

Step 5Keep resources and storage reproducible#

No external resource or migration files are declared. Any inline migration, seed data or local JSON is present in the code above. Runtime application data and private preferences are not included in the archive.

Step 6Check, run and test the result#

Save all files and run Check. Resolve diagnostics before Run. Developer CLI users can work from the extracted folder:

Developer CLI
mcbasic check ValidationProvider.mcproj
mcbasic run ValidationProvider.mcproj

Acceptance checklist

  • Enter a short nonempty email and run validation.
  • Enter an age below 18; verify the age rule reports an error.
  • Enter a malformed phone number; the mask’s validity remains part of the final decision.
  • Correct the fields and validate again: stale custom errors must clear.

Step 7Make one change and repeat the tests#

Add a business rule for an upper age limit without bypassing NumericBox.IsValid.

Save a working Git checkpoint first. Change one behavior, Check again and repeat the acceptance checklist. Compare with the downloaded solution if a regression appears.

Source baseline 20f4bfed6b · Build manifest