Page 1 of 1

Strip from string: anything that is not a digit

Posted: Sat Oct 28, 2023 10:29 pm
by StarWarsFan
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!

Re: Strip from string: anything that is not a digit

Posted: Sat Oct 28, 2023 11:06 pm
by STARGÅTE
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.

Re: Strip from string: anything that is not a digit

Posted: Sat Oct 28, 2023 11:07 pm
by Little John
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:

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

Posted: Sun Oct 29, 2023 12:51 am
by BarryG
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.

Re: Strip from string: anything that is not a digit

Posted: Sun Oct 29, 2023 12:57 am
by Kuron
BarryG 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.
+1

Re: Strip from string: anything that is not a digit

Posted: Sun Oct 29, 2023 3:21 am
by AZJIO
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

Posted: Sun Oct 29, 2023 3:49 am
by BarryG
AZJIO wrote: Sun Oct 29, 2023 3:21 amUse the #EN_CHANGE event, which will be executed when the input field changes
Or for cross-platform you can use check for #PB_EventType_Change after EventType().

Re: Strip from string: anything that is not a digit

Posted: Mon Oct 30, 2023 2:53 am
by AZJIO

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