Sign in to follow this  
Followers 0
Merchants

how to detected only number?

22 posts in this topic

#1 ·  Posted (edited)

how to detected only number?

<1234567890>

Global $i = "89"
HotKeySet("1", "Set_True_Number")
HotKeySet("2", "Set_False_Number")
HotKeySet("3", "Start")

While 1
WEnd

Func Set_True_Number()
    $i = "84"
EndFunc

Func Set_False_Number()
    $i = "e2"
EndFunc

Func Start()
    If $i = <Only Number> Then
        MsgBox(64, "Reading $i", "$i has only number")
    Else
        MsgBox(64, "Reading $i", "$i has number and words")
    EndIf
EndFunc
Edited by Merchants

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Function start should look like that.

Func Start()
    If VarGetType($i) = "Int32" Then
        MsgBox(64, "Reading $i", "$i has only number")
    ElseIf VarGetType($i) = "String" Then
        MsgBox(64, "Reading $i", "$i has number and words")
    EndIf
EndFunc

PS: Make a main loop or your script will close fast.

Edited by Andreik

When the words fail... music speaks

Share this post


Link to post
Share on other sites

How about the AutoIt functions "IsInt" or "IsNumber"?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

How about the AutoIt functions "IsInt" or "IsNumber"?

IsNumber will fail in this case because '-2' is number even if is stored as string.

When the words fail... music speaks

Share this post


Link to post
Share on other sites

IsNumber() could be used with some pre-processing

$aRtn = StringRegExp("-1243abc", "-?(.+), 1)
If NOT @Error Then
    MsgBox(0, "Result, IsNumber($aRtn[0]))
EndIf

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

IsNumber will happily return True for floatting point, so IsNumber(-12.3456e5) --> True. Hence it's not exactly what we want here (only 0..9 digits).

OTOH, IsInt may give wrong results if we insist on a digits-only input: IsInt(-123) --> True. Imagine that for instance, the OP is looking for validating a part reference that needs to be only digits, but can be long, then things will fail as well:

"1245887920309015374501650047186105301480980890408904168048048"

@Merchants, will your input be a string representing a number or an AutoIt numeric type?

If you need only 0..9 digits in your input, then try this:

Local $input[5] = [ _
    "+123.456E-5", _
    -12345, _
    +0.0002, _
    "12345", _
    "abcdef" _
]
For $inp In $input
    ConsoleWrite($inp & ' --> ' & StringRegExp(String($inp), "\A\d+\z", 0) & @LF)
Next

Time to answer the phone and others poster get faster to answer!

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

@Merchants, will your input be a string representing a number or an AutoIt numeric type?

a normal one: GuiCtrlCreateInput("", 75, 72, 150, 20)

Share this post


Link to post
Share on other sites

I see, so that you really need filtering as it can contain about everything and in Unicode, that means a lot!

My little example code should work for you (you asked for strictly 0..9, you got it) in all cases. You can drop forcing input to string as it will be a string anyway in your case.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Merchants,

Here is an input that will only accept numbers. It will allow a leading "minus" and up to 2 decimal places:

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

GUICreate("Input Filter", 300, 30, -1, -1)
Global $inTest = GUICtrlCreateInput("", 5, 5, 290)
GUIRegisterMsg($WM_COMMAND, "MY_WM_COMMAND")
GUISetState(@SW_SHOW)

While GUIGetMsg() <> $GUI_EVENT_CLOSE
WEnd

GUIRegisterMsg($WM_COMMAND, "")

Func MY_WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)

    Local $iIDFrom = BitAND($wParam, 0xFFFF);LoWord
    Local $iCode = BitShift($wParam, 16)    ;HiWord

    If $iIDFrom = $inTest And $iCode = $EN_CHANGE Then

    $Read_Input = GUICtrlRead($inTest)
    If StringRegExp($Read_Input, '[^\d.-]|([{0-9,1}^\A-])[^\d.]') Then $Read_Input = StringRegExpReplace($Read_Input, '[^\d.-]|([{0-9,1}^\A-])[^\d.]', '\1')
    $Point1 = StringInStr($Read_Input, ".", 0)
    $Point2 = StringInStr($Read_Input, ".", 0, 2)
    If $Point2 <> 0 Then $Read_Input = StringLeft($Read_Input, $Point2 - 1)
    If $Point1 <> 0 Then $Read_Input = StringLeft($Read_Input, $Point1 + 2)
    GUICtrlSetData($inTest, $Read_Input)

    EndIf

EndFunc;==>_WM_COMMAND

You might find it useful. :(

M23

Edit: Typnig!

Edited by Melba23

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Merchants,

Here is an input that will only accept numbers. It will allow a leading "minus" and up to 2 decimal places:

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

GUICreate("Input Filter", 300, 30, -1, -1)
Global $inTest = GUICtrlCreateInput("", 5, 5, 290)
GUIRegisterMsg($WM_COMMAND, "MY_WM_COMMAND")
GUISetState(@SW_SHOW)

While GUIGetMsg() <> $GUI_EVENT_CLOSE
WEnd

GUIRegisterMsg($WM_COMMAND, "")

Func MY_WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)

    Local $iIDFrom = BitAND($wParam, 0xFFFF);LoWord
    Local $iCode = BitShift($wParam, 16)    ;HiWord

    If $iIDFrom = $inTest And $iCode = $EN_CHANGE Then

    $Read_Input = GUICtrlRead($inTest)
    If StringRegExp($Read_Input, '[^\d.-]|([{0-9,1}^\A-])[^\d.]') Then $Read_Input = StringRegExpReplace($Read_Input, '[^\d.-]|([{0-9,1}^\A-])[^\d.]', '\1')
    $Point1 = StringInStr($Read_Input, ".", 0)
    $Point2 = StringInStr($Read_Input, ".", 0, 2)
    If $Point2 <> 0 Then $Read_Input = StringLeft($Read_Input, $Point2 - 1)
    If $Point1 <> 0 Then $Read_Input = StringLeft($Read_Input, $Point1 + 2)
    GUICtrlSetData($inTest, $Read_Input)

    EndIf

EndFunc;==>_WM_COMMAND

You might find it useful. :(

M23

Edit: Typnig!

uhm it is maby a script but this was not wat i was looking for here by you can use the GUI Control Styles: GuiCtrlCreateInput("", 75, 72, 150, 20, 0x2000)

Share this post


Link to post
Share on other sites

try to look back in post 1

Share this post


Link to post
Share on other sites

Possibility #4069

In The MsgLoop for the GUI

While 1
    If NOT StringRegExp(StringRight(GUICtrlRead($hInput), 1),"[\d.\-]") Then GUICtrlSetData($hInput, StringTrimRight(GUICtrlRead($hInput), 1))
$hMsg = GUIGetMsg()
 Switch $hMsg
  ;;;; Blah, blah, blah

WEnd

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

Possibility #4069

In The MsgLoop for the GUI

While 1
    If NOT StringRegExp(StringRight(GUICtrlRead($hInput), 1),"[\d.\-]") Then GUICtrlSetData($hInput, StringTrimRight(GUICtrlRead($hInput), 1))
$hMsg = GUIGetMsg()
 Switch $hMsg
  ;;;; Blah, blah, blah

WEnd

Make a example

because i don't not realy get it

#include <GUIConstantsEx.au3>
Global $GUI[4]
Start()
Func Start()
    $GUI[0]= GUICreate("test", 300, 70)
    $GUI[1] = GuiCtrlCreateInput("", 75, 22, 150, 20)
    $GUI[2] = GUICtrlCreateButton("Set", 230, 21, 50, 22)
    GUICtrlCreateLabel("Index 1:", 20, 25)
    GUISetState()
    While 1
        $GUI[3] = GUIGetMsg($GUI[0])
        Select
            Case $GUI[3] = $GUI_EVENT_CLOSE
                Exit
            Case $GUI[3] = $GUI[1]
                Start1()
        EndSelect
    WEnd
EndFunc

Func Start1()
    If StringRight(GUICtrlRead($GUI[2]), 2) = <Only Number> Then
        MsgBox(64, "Reading $i", "$i has only number")
    Else
        MsgBox(64, "Reading $i", "$i has number and words")
    EndIf
EndFunc
Edited by Merchants

Share this post


Link to post
Share on other sites

Are you home with that:

#include <GUIConstantsEx.au3>
Global $GUI[4]
Start()
Func Start()
    $GUI[0]= GUICreate("test", 300, 70)
    $GUI[1] = GuiCtrlCreateInput("", 75, 22, 150, 20)
    $GUI[2] = GUICtrlCreateButton("Set", 230, 21, 50, 22)
    GUICtrlCreateLabel("Index 1:", 20, 25)
    GUISetState()
    While 1
    $GUI[3] = GUIGetMsg($GUI[0])
    Select
    Case $GUI[3] = $GUI_EVENT_CLOSE
    Exit
    Case $GUI[3] = $GUI[1]
    Start1()
    EndSelect
    WEnd
EndFunc

Func Start1()
    If StringRegExp(StringRight(GUICtrlRead($GUI[1]), 2), "\A\d+\z", 0) Then
    MsgBox(64, "Reading $i", "The two rightmost characters of input = " & StringRight(GUICtrlRead($GUI[1]), 2) & " are only 0..9")
    Else
    MsgBox(64, "Reading $i", "The two rightmost characters of input = " & StringRight(GUICtrlRead($GUI[1]), 2) & " are 0..9 and/or anything else")
    EndIf
EndFunc

(Beware the indexing, GUICtrlRead($GUI[1])


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

Another example.

Global $i = 89

HotKeySet("1", "Set_True_Number")
HotKeySet("2", "Set_False_Number")
HotKeySet("3", "Start")
HotKeySet("{ESC}", "Terminate")

While 1
    Sleep(10) ; Reduces CPU usage.
WEnd

Func Set_True_Number()
    $i = "84"
    Start()
EndFunc ;==>Set_True_Number

Func Set_False_Number()
    $i = "e2"
    Start()
EndFunc ;==>Set_False_Number

Func Terminate()
    Exit 0
EndFunc ;==>Terminate


Func Start()
    If StringRegExp($i, "[0-9]{" & StringLen($i) & "}") Then ; True only when all characters are digits (0 to 9).
        MsgBox(64, "Reading $i", $i & " is only number", 2)
    Else
        MsgBox(64, "Reading $i", $i & " is number and words", 2)
    EndIf
EndFunc ;==>Start

Share this post


Link to post
Share on other sites

#16 ·  Posted (edited)

Another example.

Global $i = 89

HotKeySet("1", "Set_True_Number")
HotKeySet("2", "Set_False_Number")
HotKeySet("3", "Start")
HotKeySet("{ESC}", "Terminate")

While 1
    Sleep(10) ; Reduces CPU usage.
WEnd

Func Set_True_Number()
    $i = "84"
    Start()
EndFunc ;==>Set_True_Number

Func Set_False_Number()
    $i = "e2"
    Start()
EndFunc ;==>Set_False_Number

Func Terminate()
    Exit 0
EndFunc ;==>Terminate


Func Start()
    If StringRegExp($i, "[0-9]{" & StringLen($i) & "}") Then ; True only when all characters are digits (0 to 9).
        MsgBox(64, "Reading $i", $i & " is only number", 2)
    Else
        MsgBox(64, "Reading $i", $i & " is number and words", 2)
    EndIf
EndFunc ;==>Start
got my self in a lite problem...

if the string = 11 Then run test good ElseIf string = 01 Then test not good

#include <GUIConstantsEx.au3>
Global $GUI[4]

$GUI[0]= GUICreate("test", 300, 70)
$GUI[1] = GuiCtrlCreateInput("01", 75, 22, 150, 20)
$GUI[2] = GUICtrlCreateButton("Set", 230, 21, 50, 22)
GUICtrlCreateLabel("Index 1:", 20, 25)
GUISetState()
While 1
    $GUI[3] = GUIGetMsg($GUI[0])
    Select
        Case $GUI[3] = $GUI_EVENT_CLOSE
            Exit
        Case $GUI[3] = $GUI[2]
            If StringRegExp(StringLeft(GUICtrlRead($GUI[1]), 2), "[1-9]") Then
                MsgBox(64, "test", "test good " & StringLeft(GUICtrlRead($GUI[1]), 2))
            Else
                MsgBox(64, "test", "test not good " & StringLeft(GUICtrlRead($GUI[1]), 2))
            EndIf
    EndSelect
WEnd
Edited by Merchants

Share this post


Link to post
Share on other sites

Did you run your own example? It works as is, but probably not the way you intend.

The regexp you use now will match if it finds one digit between 1 and 9 anywhere in the string or rather within the leftmost 2 characters of the string, due to the StringLeft() call.

So it matches '01', '11', '*3whatever you want', '5', ...

It barks on '00', 'v0', ...


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

#18 ·  Posted (edited)

Did you run your own example? It works as is, but probably not the way you intend.

The regexp you use now will match if it finds one digit between 1 and 9 anywhere in the string or rather within the leftmost 2 characters of the string, due to the StringLeft() call.

So it matches '01', '11', '*3whatever you want', '5', ...

It barks on '00', 'v0', ...

uhm ok wel i found a way to get that good

If StringRegExp(StringLeft(StringRight(GUICtrlRead($GUI[1]), 2), 1), "[1-9]") Then
    MsgBox(64, "test", "test good")
Else
    MsgBox(64, "test", "test not good")
EndIf
Edited by Merchants

Share this post


Link to post
Share on other sites

Sound overcomplex to me. What, precisely are your requirement? Your first post said that the string had to be only digis 0..9, then it was the first 2 chars in 1..9, now only the second last in 1..9.

I'm still unsure that what you've come up with will work in all cases: "My memory size is 8G" matches fine your last version. Is that what you want?


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

I think the OP better start all over again with what he wants here. Comments like "read my first post again" just don't cut it because that code was a total mess and it will hurt to read it again.

Next issue, although it will work, why are you creating the GUI as an array?

Third, if you are using 0x2000 as the style then all of this bit of making sure that it will accept negative numbers or decimal places is for naught. 0x2000 says to accept ONLY digits so that style is useless unless he wants a positive whole number in which case he needs nothing other than the style.

Fourth, if he wants to use negative numbers or numbers with decimal places then he already has SRE examples given that will do that. Since it's an input box and assumably he is typing data into the input box then the only characters he has to check are the first (for a minus sign) and the last to make sure it's either a decimal point OR a digit. The one thing I forgot to allow for was some idiot typing in more than one decimal point or the situation where it starts with a decimal (-? should have been [-.]?)

Don't understand this, read my first sentence again.


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
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
Sign in to follow this  
Followers 0