Jump to content

stringregexp in real time


marko001
 Share

Go to solution Solved by mikell,

Recommended Posts

Hi all guys,

i'm trying to use the .au3 i found on

Since in my application I have several inputboxes, is there a way to manage all of them together like a variable set at beginning?

I need 2 kind of bindings:

a) only numbers from 0 to 50

B) only digits with "." or "," (currency)

I need to avoid errors, ie I can't use d. since it could allow 33.333.3.33  and I just need decimal divisor.

Thanks guys,

Marco

Link to comment
Share on other sites

marko001,

Since in my application I have several inputboxes, is there a way to manage all of them together like a variable set at beginning?

 

Don't know unless you post the script or a representative example.

edit: Incidentally, the post you refer to is severely out of date.

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Realtime is funny  :)

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3> 
#include <EditConstants.au3>

Global $array[3]

$gui = GUICreate("test", 300, 200)
$array[0] = GUICtrlCreateInput("", 20, 20, 200, 22)
$array[1] = GUICtrlCreateInput("", 20, 60, 200, 22)
$array[2] = GUICtrlCreateInput("", 20, 100, 200, 22)
GUISetState(@SW_SHOWNA)

GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND") 

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
Wend


Func _WM_COMMAND($hWnd, $Msg, $wParam, $lParam)  
 #forceref $hWnd, $Msg, $lParam
For $i = 0 to 2
 Switch BitAND($wParam, 0x0000FFFF)
    Case $array[$i]
        If BitShift($wParam, 16) = $EN_UPDATE Then
           Local $txt = GuiCtrlRead($array[$i])
           If StringRegExp($txt, '[^.]*\.[^.]+') AND StringRight($txt, 1) = "." OR _ 
              StringRegExp($txt, '[^,]*,[^,]+') AND StringRight($txt, 1) = ","  OR _ 
              StringRegExp($txt, '[^\d.,]')  OR _ 
              StringLen($txt) > 5  OR _ 
              Number($txt) > 50 _ 
                    Then GuiCtrlSetData($array[$i], StringTrimRight($txt, 1))
           If $txt = "." Then GuiCtrlSetData($array[$i], "0.")
           If $txt = "," Then GuiCtrlSetData($array[$i], "0,")
        EndIf
 EndSwitch
Next
  Return $GUI_RUNDEFMSG
EndFunc  ;==> _WM_COMMAND
Link to comment
Share on other sites

I have a merchant and need customers to input 2 different values:

$item1value = GUICtrlCreateInput("", 712, 59, 40, 23) ; this is currency and can be xx.yy $ OR xx,yy €

 

and

$item1quantity = GUICtrlCreateInput("", 136, 324, 33, 21, $ES_NUMBER) ; this is an integer from 1 to 50

 

I have more than 100 items so I would like to have one/two funcs that could manage the whole thing without setting for every input its regexp

But I need to be sure customer can input for example 51.99 (non possible in the example you suggested).

Thanks again,

Marco

Link to comment
Share on other sites

Looking at my code I saw that for other values only integer it's enough, withouth binding to 50.

So at the end, I calculated all my inputboxes..., i have

120 currency inputboxes

120 quantity inputboxes (q.ty >=50)

10 integer inputboxes (0<x<5000)

How to manage all of them? One by one or?

Thanks again,

Marco

[Edit] And I saw that i can enter more than one dot "." this leads to an error if system gets it. (i.e. 34.0.)

Is there a way to solve it?

Edited by marko001
Link to comment
Share on other sites

  • Solution

You won't manage 250 input boxes one by one  :)

Use an array to store the IDs then use a loop to check them inside the WM_COMMAND

As for each item_value there is an item_quantity, an array 2D seems to be a good idea

If the code below doesn't exactly fit your needs then you can easily make the wanted small modifs in the condition(s)

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3> 
#include <EditConstants.au3>

Global $item[2][2]
Global $integer[2]

$gui = GUICreate("test", 300, 200)
$item[0][0] = GUICtrlCreateInput("", 20, 20, 100, 22)   ; item_value
$item[0][1] = GUICtrlCreateInput("", 150, 20, 80, 22, $ES_NUMBER)   ; item_quantity
$item[1][0] = GUICtrlCreateInput("", 20, 50, 100, 22)
$item[1][1] = GUICtrlCreateInput("", 150, 50, 80, 22, $ES_NUMBER)

$integer[0] = GUICtrlCreateInput("", 20, 100, 150, 22, $ES_NUMBER)   ; integer 
$integer[1] = GUICtrlCreateInput("", 20, 130, 150, 22, $ES_NUMBER)  
GUISetState(@SW_SHOWNA)

GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND") 

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
Wend


Func _WM_COMMAND($hWnd, $Msg, $wParam, $lParam)  
 #forceref $hWnd, $Msg, $lParam

Local $id = BitAND($wParam, 0x0000FFFF)
Local $code = BitShift($wParam, 16)

For $i = 0 to UBound($item)-1
 Switch $id
    Case $item[$i][0]       ; item_value
        If $code = $EN_UPDATE Then
            Local $txt = GuiCtrlRead($item[$i][0])
            If StringRegExp($txt, '[^\d.,]') OR StringLen($txt) > 5 _ 
                    Then GuiCtrlSetData($item[$i][0], StringTrimRight($txt, 1))
            If  StringRight($txt, 1) = "." OR StringRight($txt, 1) = "," Then
                   StringRegExpReplace($txt, '[.,]', "")
                   If @extended > 1 Then GuiCtrlSetData($item[$i][0], StringTrimRight($txt, 1))
            EndIf
            If $txt = "." Then GuiCtrlSetData($item[$i][0], "0.")
            If $txt = "," Then GuiCtrlSetData($item[$i][0], "0,")
        EndIf
    Case $item[$i][1]        ; item_quantity
        If $code = $EN_UPDATE Then
           Local $txt = GuiCtrlRead($item[$i][1])
           If Number($txt) > 50 OR Number($txt) < 1 Then GuiCtrlSetData($item[$i][1], StringTrimRight($txt, 1))
        EndIf
 EndSwitch
Next

For $i = 0 to UBound($integer)-1
 Switch $id
    Case $integer[$i]          ; integer 
        If $code = $EN_UPDATE Then
           Local $txt = GuiCtrlRead($integer[$i])
           If Number($txt) > 5000 OR Number($txt) < 1 Then GuiCtrlSetData($integer[$i], StringTrimRight($txt, 1))
        EndIf
 EndSwitch
Next

  Return $GUI_RUNDEFMSG
EndFunc  ;==> _WM_COMMAND
Link to comment
Share on other sites

Yes, it works perfectly.

I added the following condition just after EndSwitch:

        If StringLen($txt) > 1 And StringLeft($txt,1) = "0"  And StringMid($txt,2,1) <> "." Then $txt = StringTrimLeft($txt,1)

 

since I don't want 01, 02....

It keeps the 0 only if there is the . as second char.

It works perfectly but... is there a way to clean the code and use RegExpr to manage it?

In any case i saw that I still can save "0." It shouldn't accept a value if after the 0. there are no digits. Can be done?

Thanks again,

marco

Link to comment
Share on other sites

This one will remove a leading 0 if not followed by dot or comma

$str = "06.45"
$str = StringRegExpReplace($str, '^0(?![.,])', "")
Msgbox(0,"", $str)

To disallow a value like "0.00" just check later the input content (difficult to manage this in real time)

$content = Number(GuiCtrlRead($item[$i][0]) )
If $content <> 0 Then ...
Edited by mikell
Link to comment
Share on other sites

Hummm...  :)

Try this (for real time use)

Local $txt = GuiCtrlRead($input)
Local $iNb = 0
Local $aNb = StringRegExp($txt, '.*(?<=^|,)(\d+)$', 3)
If IsArray($aNb) Then $iNb = Number($aNb[0] )
If StringRegExp($txt, '[^\d,]') OR _   ;  allow \d or comma only
    StringRegExp($txt, '.*,,') OR _        ;  disallow 2 consecutive commas
    $iNb > 50 _   ;  disallow last number > 50
        Then GuiCtrlSetData($input, StringTrimRight($txt, 1))
Edited by mikell
Link to comment
Share on other sites

Syntax Error? 

In

 Number(StringRegExp($txt, '.*(?<=^|,)(\d+)$', 3)[0]) > 50 _ 

,3)[0]) ?

M.

I had to declare the array locally:

Local $AR1 = StringRegExp($txt, '.*(?<=^|,)(d+)$', 3)

Number($AR1[0]) > 50 _

but it gives me error when inputbox is empty or i start writing:

F:\AUTO-IT Projects\__27082014\FunzioniDiSupporto.au3 (282) : ==> Subscript used with non-Array variable.:
If StringRegExp($txt, '[^\d,]') Or StringRegExp($txt, '.*,,') Or Number($AR1[0]) > 50 Then $txt = StringTrimRight($txt, 1)
If StringRegExp($txt, '[^\d,]') Or StringRegExp($txt, '.*,,') Or Number($AR1^ ERROR
Edited by marko001
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...