I am trying to find a solution for the following:
Given I have StringGadget IN111 where the user is to put in some data in format integer.
I would then get the input result using in$= GetGadgetText(IN111) and put that into variable x for further calculation using x= val(in$)
Let us assume that the user unwillingly hits other keys than he wants so what he enter is 100,g000
He was to put in 100,000, but an extra "g"-character got in the way.
You see where I am going?
I need to strip from that string everything that is not a digit, so I can continue with calculations. Anyone got a solution for this?
Any constructive response greatly appreciated. Thanks for your time, fellows!
Strip from string: anything that is not a digit
-
- Enthusiast
- Posts: 169
- Joined: Sat Mar 14, 2015 11:53 am
Re: Strip from string: anything that is not a digit
You can use a regular expression:
Code: Select all
Enumeration
#RegularExpression
EndEnumeration
CreateRegularExpression(#RegularExpression, "\D+") ; The \D stands for all characters except digits 0 to 9
Debug ReplaceRegularExpression(#RegularExpression, "100,g00k0", "") ; Replace the pattern by an empty string.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and more ― Typeface - Sprite-based font include/module
Lizard - Script language for symbolic calculations and more ― Typeface - Sprite-based font include/module
-
- Addict
- Posts: 4770
- Joined: Thu Jun 07, 2007 3:25 pm
- Location: Berlin, Germany
Re: Strip from string: anything that is not a digit
I would silently remove all "," characters.
For wrong characters such as "g", I would issue a warning so that the user can correct the error.
E.g. like this:
For wrong characters such as "g", I would issue a warning so that the user can correct the error.
E.g. like this:
Code: Select all
EnableExplicit
#AllowedChars$ = "0123456789,"
Procedure.i CheckIntegerInput (in$)
Protected char$
Define.i length, posn
length = Len(in$)
For posn = 1 To length
char$ = Mid(in$, posn, 1)
If FindString(#AllowedChars$, char$) = 0
MessageRequester("Error", "Invalid character: " + char$)
ProcedureReturn 0
EndIf
Next
ProcedureReturn Val(RemoveString(in$, ","))
EndProcedure
Debug CheckIntegerInput("100,g000")
Debug CheckIntegerInput("100000.0")
Debug CheckIntegerInput("100,000")
Re: Strip from string: anything that is not a digit
Don't forget that StringGadgets() have a flag called #PB_String_Numeric, to prevent non-digits being typed in. It's in the manual. But this won't stop non-numerics being pasted in from the clipboard.
I personally wait until the user hits Enter on the StringGadget() and then correct anything that is not valid, and use SetGadgetText() to put the corrected text in it. This is a nice convenience feature so that the user doesn't have to re-type anything or dismiss an error message. It reminds them of the format required without actively bothering them.
I personally wait until the user hits Enter on the StringGadget() and then correct anything that is not valid, and use SetGadgetText() to put the corrected text in it. This is a nice convenience feature so that the user doesn't have to re-type anything or dismiss an error message. It reminds them of the format required without actively bothering them.
Re: Strip from string: anything that is not a digit
+1BarryG wrote: Sun Oct 29, 2023 12:51 am I personally wait until the user hits Enter on the StringGadget() and then correct anything that is not valid, and use SetGadgetText() to put the corrected text in it. This is a nice convenience feature so that the user doesn't have to re-type anything or dismiss an error message. It reminds them of the format required without actively bothering them.
Best wishes to the PB community. Thank you for the memories. 
Re: Strip from string: anything that is not a digit
Use the #EN_CHANGE event, which will be executed when the input field changes, and check using a regular expression. I have to go to work. The problem is standard on the AutoIt3 forum and has been solved 100 times, but I can’t find my example now that takes into account the decimal part of a number.
Re: Strip from string: anything that is not a digit
Or for cross-platform you can use check for #PB_EventType_Change after EventType().AZJIO wrote: Sun Oct 29, 2023 3:21 amUse the #EN_CHANGE event, which will be executed when the input field changes
Re: Strip from string: anything that is not a digit
Code: Select all
EnableExplicit
#Window = 0
Enumeration Gadget
#StrG
#btn
EndEnumeration
Enumeration RegExp
#RegExp
#RegExp2
EndEnumeration
Global tmp$, Text$, Count, start1, end1
Global Dim Res.s(0)
If Not CreateRegularExpression(#RegExp2, "\A\d{1,3}(,\d{3})*(\.\d+)?", #PB_RegularExpression_NoCase)
Debug RegularExpressionError()
EndIf
If Not CreateRegularExpression(#RegExp, "\A (\d{1,3} (,|,\d{1,3}|,\d{3}(\.|\.\d+)?)*? |\d{1,3}(\.|\.\d+)?) \z", #PB_RegularExpression_NoCase | #PB_RegularExpression_Extended)
Debug RegularExpressionError()
EndIf
If OpenWindow(#Window, 0, 0, 220, 100, "Example...", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
StringGadget(#StrG, 5 , 5, 200, 30, "")
ButtonGadget(#btn, 10, 50, 200, 30, "Check")
SetActiveGadget(#StrG)
Repeat
Select WaitWindowEvent()
Case #PB_Event_Gadget
Select EventGadget()
Case #StrG
If EventType() = #PB_EventType_Change
Text$ = GetGadgetText(#StrG)
If Asc(Text$)
Count = ExtractRegularExpression(#RegExp, Text$, Res())
If Count > 0
tmp$ = Res(0)
EndIf
If tmp$ <> Text$
SendMessage_(GadgetID(#StrG), #EM_GETSEL, @start1, @end1)
SetGadgetText(#StrG, tmp$)
start1 - 1
SendMessage_(GadgetID(#StrG), #EM_SETSEL, start1, start1)
; keybd_event_(#VK_CONTROL,0,#KEYEVENTF_EXTENDEDKEY,0)
; Delay(10)
; keybd_event_(#VK_RIGHT,0,#KEYEVENTF_EXTENDEDKEY,0)
; Delay(10)
; keybd_event_(#VK_RIGHT,0,#KEYEVENTF_KEYUP | #KEYEVENTF_EXTENDEDKEY,0)
; Delay(10)
; keybd_event_(#VK_CONTROL,0,#KEYEVENTF_KEYUP | #KEYEVENTF_EXTENDEDKEY,0)
EndIf
EndIf
EndIf
Case #btn
Text$ = GetGadgetText(#StrG)
If Asc(Text$)
Count = ExtractRegularExpression(#RegExp2, Text$, Res())
If Count > 0
tmp$ = Res(0)
EndIf
If tmp$ <> Text$
SetGadgetText(#StrG, tmp$)
EndIf
EndIf
EndSelect
Case #PB_Event_CloseWindow
CloseWindow(#Window)
End
EndSelect
ForEver
EndIf