Date: 02-16-2022
Return to Index
created by gbSnippets
'Programmers often need to check that a user's input has only specified
'characters. The PowerBASIC VERIFY function will do that.
'Primary Code:
'Syntax: x = VERIFY([start&,] MainString, MatchString)
'returns 0 if all characters in MatchString are in MainString
'otherwise, returns position of 1st non-matching character in MainString
'Examples
Dim x&
x& = Verify("Hello", "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") 'check for letters only, returns 0
x& = Verify("Hello", Chr$(65 to 90, 97 to 122)) 'check for letters only, returns 0, uses Chr$ to ease the typing burden, although is harder to read
x& = Verify("742.94", "0123456789") 'check for numbers only, returns 4. the "." is not a match.
x& = Verify("-742.94", ".+-0123456789") 'check for numbers, and +-. chars, returns 0. the "." is a match.
x& = Verify("742.94", Chr$(48 to 57)) 'check for numbers only, returns 4. the "." is not a match, uses Chr$ to create number list
'Compilable Example: (Jose Includes)
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
Global hDlg As DWord
Function PBMain() As Long
Dialog New Pixels, 0, "Test Code",300,300,200,200, %WS_OverlappedWindow To hDlg
Control Add Button, hDlg, 100,"Check for Numbers", 50,10,100,20
Control Add Button, hDlg, 200,"Check for Letters", 50,40,100,20
Control Add TextBox, hDlg, 300,"<text here>", 50,70,100,20
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Local temp$, iResult as Long
If CB.Msg = %WM_Command AND CB.Ctl = 100 AND CB.Ctlmsg = %BN_Clicked Then
Control Get Text hDlg, 300 To temp$
Select Case Verify(temp$, Chr$(48 to 57,".")) 'check for numbers and period
Case %True: MsgBox "Error. non-numbers found"
Case %False: MsgBox "Success. only numbers found"
End Select
End If
If CB.Msg = %WM_Command AND CB.Ctl = 200 AND CB.Ctlmsg = %BN_Clicked Then
Control Get Text hDlg, 300 To temp$
Select Case Verify(temp$, Chr$(65 to 90, 97 to 122)) 'check for letters only
Case %True: MsgBox "Error. non-letters found"
Case %False: MsgBox "Success. only letters found"
End Select
End If
End Function
'gbs_00245
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm